<!-- build time:Wed Apr 08 2020 11:46:45 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html class="theme-next pisces use-motion" lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="theme-color" content="#222"><script src="//cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script><link href="//cdn.jsdelivr.net/npm/pace-js@1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet"><script>!function(e,t,o,c,i,a,n){e.DaoVoiceObject=i,e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=1*new Date,a=t.createElement(o),n=t.getElementsByTagName(o)[0],a.async=1,a.src=c,a.charset="utf-8",n.parentNode.insertBefore(a,n)}(window,document,"script",("https:"==document.location.protocol?"https:":"http:")+"//widget.daovoice.io/widget/0f81ff2f.js","daovoice"),daovoice("init",{app_id:"6f8389c1"}),daovoice("update")</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="https://fonts.loli.net/css?family=EB+Garamond:400,400i,700,700i|Noto+Serif+SC:400,500,700&display=swap&subset=chinese-simplified" rel="stylesheet"><link href="//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"><link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css"><link rel="apple-touch-icon" sizes="180x180" href="https://gitee.com/sky_mirrors_the_clouds/cloudimg/raw/master/https://gitee.com/sky_mirrors_the_clouds/cloudimg/apple-touch-icon-next.png?v=5.1.4"><link rel="icon" type="image/png" sizes="32x32" href="https://gitee.com/sky_mirrors_the_clouds/cloudimg/raw/master/https://gitee.com/sky_mirrors_the_clouds/cloudimg/favicon-32x32-next.png?v=5.1.4"><link rel="icon" type="image/png" sizes="16x16" href="https://gitee.com/sky_mirrors_the_clouds/cloudimg/raw/master/https://gitee.com/sky_mirrors_the_clouds/cloudimg/favicon-16x16-next.png?v=5.1.4"><link rel="mask-icon" href="https://gitee.com/sky_mirrors_the_clouds/cloudimg/raw/master/https://gitee.com/sky_mirrors_the_clouds/cloudimg/logo.svg?v=5.1.4" color="#222"><meta name="keywords" content="pandas,"><link rel="alternate" href="/blog/atom.xml" title="天镜云生" type="application/atom+xml"><meta name="description" content="官方网站上《10 Minutes to pandas》的一个简单的翻译，原文在这里。这篇文章是对 pandas 的一个简单的介绍，详细的介绍请参考：秘籍 。习惯上，我们会按下面格式引入所需要的包：12345In [1]: import pandas as pdIn [2]: import numpy as npIn [3]: import matplotlib.pyplot as plt一、 创建"><meta property="og:type" content="article"><meta property="og:title" content="十分钟搞定 pandas"><meta property="og:url" content="http://sky_mirrors_the_clouds.gitee.io/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html"><meta property="og:site_name" content="天镜云生"><meta property="og:description" content="官方网站上《10 Minutes to pandas》的一个简单的翻译，原文在这里。这篇文章是对 pandas 的一个简单的介绍，详细的介绍请参考：秘籍 。习惯上，我们会按下面格式引入所需要的包：12345In [1]: import pandas as pdIn [2]: import numpy as npIn [3]: import matplotlib.pyplot as plt一、 创建"><meta property="og:image" content="http://pandas.pydata.org/pandas-docs/stable/_images/series_plot_basic.png"><meta property="og:image" content="http://pandas.pydata.org/pandas-docs/stable/_images/frame_plot_basic.png"><meta property="article:published_time" content="2020-03-27T16:00:00.000Z"><meta property="article:modified_time" content="2020-04-04T08:35:09.041Z"><meta property="article:author" content="TJYS"><meta property="article:tag" content="pandas"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="http://pandas.pydata.org/pandas-docs/stable/_images/series_plot_basic.png"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/blog/",scheme:"Pisces",version:"5.1.4",sidebar:{position:"left",display:"post",offset:12,b2t:!1,scrollpercent:!0,onmobile:!0},fancybox:!1,tabs:!0,motion:{enable:!0,async:!1,transition:{post_block:"flipYIn",post_header:"perspectiveRightIn",post_body:"perspectiveLeftIn",coll_header:"perspectiveDownIn",sidebar:"perspectiveUpIn"}},duoshuo:{userId:"0",author:"博主"},algolia:{applicationID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}}}</script><link rel="canonical" href="http://sky_mirrors_the_clouds.gitee.io/blog/十分钟搞定pandas.html"><script></script><title>十分钟搞定 pandas | 天镜云生</title><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.1/js/bubble.js"></script><meta name="generator" content="Hexo 4.2.0"></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans"><script type="text/javascript" src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script><script type="text/javascript">var windowWidth=$(window).width();windowWidth>480&&document.write('<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.1/js/snow.js"><\/script>')</script><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><a href="https://github.com/Qikaile" target="_blank" rel="noopener external nofollow noreferrer" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513;color:#fff;position:absolute;top:0;border:0;right:0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/blog/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">天镜云生</span> <span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">Welcome!</h1></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br>首页</a></li><li class="menu-item menu-item-about"><a href="/blog/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i><br>关于</a></li><li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i><br>标签</a></li><li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i><br>分类</a></li><li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br>归档</a></li><li class="menu-item menu-item-shuoshuo"><a href="/blog/shuoshuo/" rel="section"><i class="menu-item-icon fa fa-fw fa-fa-commenting-o fa-commenting"></i><br>说说</a></li><li class="menu-item menu-item-sitemap"><a href="/blog/sitemap.xml" rel="section"><i class="menu-item-icon fa fa-fw fa-sitemap"></i><br>站点地图</a></li><li class="menu-item menu-item-search"><a href="javascript:;" rel="external nofollow noreferrer" class="popup-trigger"><i class="menu-item-icon fa fa-search fa-fw"></i><br>搜索</a></li></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="http://sky_mirrors_the_clouds.gitee.io/blog/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="TJYS"><meta itemprop="description" content=""><meta itemprop="image" content="/blog/images/avatar.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="天镜云生"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">十分钟搞定 pandas</h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-28T00:00:00+08:00">2020-03-28 </time></span><span class="post-category"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-folder-o"></i> </span><span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/blog/categories/pandas/" itemprop="url" rel="index"><span itemprop="name">pandas</span> </a></span></span><span class="post-comments-count"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><a href="/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html" itemprop="commentCount"></span> </a></span><span class="post-wordcount"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">字数&#58;</span> <span title="字数">6.1k </span><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">时长 &asymp;</span> <span title="时长">33 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><p>官方网站上《10 Minutes to pandas》的一个简单的翻译，原文在<a href="http://pandas.pydata.org/pandas-docs/stable/10min.html" target="_blank" rel="noopener external nofollow noreferrer">这里</a>。这篇文章是对 pandas 的一个简单的介绍，详细的介绍请参考：<a href="http://pandas.pydata.org/pandas-docs/stable/cookbook.html#cookbook" target="_blank" rel="noopener external nofollow noreferrer">秘籍</a> 。习惯上，我们会按下面格式引入所需要的包：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]: <span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line">In [<span class="number">2</span>]: <span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">In [<span class="number">3</span>]: <span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br></pre></td></tr></table></figure></div><h1 id="一、-创建对象"><a href="#一、-创建对象" class="headerlink" title="一、 创建对象"></a>一、 创建对象</h1><p>可以通过 <a href="http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dsintro" target="_blank" rel="noopener external nofollow noreferrer">数据结构入门</a> 来查看有关该节内容的详细信息。</p><p>1、可以通过传递一个<code>list</code>对象来创建一个<code>Series</code>，pandas 会默认创建整型索引：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">4</span>]: s = pd.Series([<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,np.nan,<span class="number">6</span>,<span class="number">8</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">5</span>]: s</span><br><span class="line">Out[<span class="number">5</span>]: </span><br><span class="line"><span class="number">0</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">1</span>    <span class="number">3.0</span></span><br><span class="line"><span class="number">2</span>    <span class="number">5.0</span></span><br><span class="line"><span class="number">3</span>    NaN</span><br><span class="line"><span class="number">4</span>    <span class="number">6.0</span></span><br><span class="line"><span class="number">5</span>    <span class="number">8.0</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、通过传递一个 numpy<code>array</code>，时间索引以及列标签来创建一个<code>DataFrame</code>：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">6</span>]: dates = pd.date_range(<span class="string">'20130101'</span>, periods=<span class="number">6</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">7</span>]: dates</span><br><span class="line">Out[<span class="number">7</span>]: </span><br><span class="line">DatetimeIndex([<span class="string">'2013-01-01'</span>, <span class="string">'2013-01-02'</span>, <span class="string">'2013-01-03'</span>, <span class="string">'2013-01-04'</span>,</span><br><span class="line">               <span class="string">'2013-01-05'</span>, <span class="string">'2013-01-06'</span>],</span><br><span class="line">              dtype=<span class="string">'datetime64[ns]'</span>, freq=<span class="string">'D'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">8</span>]: df = pd.DataFrame(np.random.randn(<span class="number">6</span>,<span class="number">4</span>), index=dates, columns=list(<span class="string">'ABCD'</span>))</span><br><span class="line"></span><br><span class="line">In [<span class="number">9</span>]: df</span><br><span class="line">Out[<span class="number">9</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span>  <span class="number">0.524988</span></span><br></pre></td></tr></table></figure></div><p>3、通过传递一个能够被转换成类似序列结构的字典对象来创建一个<code>DataFrame</code>：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">10</span>]: df2 = pd.DataFrame(&#123; <span class="string">'A'</span> : <span class="number">1.</span>,</span><br><span class="line">   ....:                      <span class="string">'B'</span> : pd.Timestamp(<span class="string">'20130102'</span>),</span><br><span class="line">   ....:                      <span class="string">'C'</span> : pd.Series(<span class="number">1</span>,index=list(range(<span class="number">4</span>)),dtype=<span class="string">'float32'</span>),</span><br><span class="line">   ....:                      <span class="string">'D'</span> : np.array([<span class="number">3</span>] * <span class="number">4</span>,dtype=<span class="string">'int32'</span>),</span><br><span class="line">   ....:                      <span class="string">'E'</span> : pd.Categorical([<span class="string">"test"</span>,<span class="string">"train"</span>,<span class="string">"test"</span>,<span class="string">"train"</span>]),</span><br><span class="line">   ....:                      <span class="string">'F'</span> : <span class="string">'foo'</span> &#125;)</span><br><span class="line">   ....: </span><br><span class="line"></span><br><span class="line">In [<span class="number">11</span>]: df2</span><br><span class="line">Out[<span class="number">11</span>]: </span><br><span class="line">     A          B    C  D      E    F</span><br><span class="line"><span class="number">0</span>  <span class="number">1.0</span> <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.0</span>  <span class="number">3</span>   test  foo</span><br><span class="line"><span class="number">1</span>  <span class="number">1.0</span> <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.0</span>  <span class="number">3</span>  train  foo</span><br><span class="line"><span class="number">2</span>  <span class="number">1.0</span> <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.0</span>  <span class="number">3</span>   test  foo</span><br><span class="line"><span class="number">3</span>  <span class="number">1.0</span> <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.0</span>  <span class="number">3</span>  train  foo</span><br></pre></td></tr></table></figure></div><p>4、查看不同列的数据类型：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">12</span>]: df2.dtypes</span><br><span class="line">Out[<span class="number">12</span>]: </span><br><span class="line">A           float64</span><br><span class="line">B    datetime64[ns]</span><br><span class="line">C           float32</span><br><span class="line">D             int32</span><br><span class="line">E          category</span><br><span class="line">F            object</span><br><span class="line">dtype: object</span><br></pre></td></tr></table></figure></div><p>5、如果你使用的是 IPython，使用 Tab 自动补全功能会自动识别所有的属性以及自定义的列，下图中是所有能够被自动识别的属性的一个子集：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">13</span>]: df2.&lt;TAB&gt;</span><br><span class="line">df2.A                  df2.boxplot</span><br><span class="line">df2.abs                df2.C</span><br><span class="line">df2.add                df2.clip</span><br><span class="line">df2.add_prefix         df2.clip_lower</span><br><span class="line">df2.add_suffix         df2.clip_upper</span><br><span class="line">df2.align              df2.columns</span><br><span class="line">df2.all                df2.combine</span><br><span class="line">df2.any                df2.combineAdd</span><br><span class="line">df2.append             df2.combine_first</span><br><span class="line">df2.apply              df2.combineMult</span><br><span class="line">df2.applymap           df2.compound</span><br><span class="line">df2.as_blocks          df2.consolidate</span><br><span class="line">df2.asfreq             df2.convert_objects</span><br><span class="line">df2.as_matrix          df2.copy</span><br><span class="line">df2.astype             df2.corr</span><br><span class="line">df2.at                 df2.corrwith</span><br><span class="line">df2.at_time            df2.count</span><br><span class="line">df2.axes               df2.cov</span><br><span class="line">df2.B                  df2.cummax</span><br><span class="line">df2.between_time       df2.cummin</span><br><span class="line">df2.bfill              df2.cumprod</span><br><span class="line">df2.blocks             df2.cumsum</span><br><span class="line">df2.bool               df2.D</span><br></pre></td></tr></table></figure></div><h1 id="二、-查看数据"><a href="#二、-查看数据" class="headerlink" title="二、 查看数据"></a>二、 查看数据</h1><p>详情请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics" target="_blank" rel="noopener external nofollow noreferrer">基础</a>。</p><p>1、 查看<code>DataFrame</code>中头部和尾部的行：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">14</span>]: df.head()</span><br><span class="line">Out[<span class="number">14</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">15</span>]: df.tail(<span class="number">3</span>)</span><br><span class="line">Out[<span class="number">15</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span>  <span class="number">0.524988</span></span><br></pre></td></tr></table></figure></div><p>2、 显示索引、列和底层的 numpy 数据：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">16</span>]: df.index</span><br><span class="line">Out[<span class="number">16</span>]: </span><br><span class="line">DatetimeIndex([<span class="string">'2013-01-01'</span>, <span class="string">'2013-01-02'</span>, <span class="string">'2013-01-03'</span>, <span class="string">'2013-01-04'</span>,</span><br><span class="line">               <span class="string">'2013-01-05'</span>, <span class="string">'2013-01-06'</span>],</span><br><span class="line">              dtype=<span class="string">'datetime64[ns]'</span>, freq=<span class="string">'D'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">17</span>]: df.columns</span><br><span class="line">Out[<span class="number">17</span>]: Index([<span class="string">u'A'</span>, <span class="string">u'B'</span>, <span class="string">u'C'</span>, <span class="string">u'D'</span>], dtype=<span class="string">'object'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">18</span>]: df.values</span><br><span class="line">Out[<span class="number">18</span>]: </span><br><span class="line">array([[ <span class="number">0.4691</span>, <span class="number">-0.2829</span>, <span class="number">-1.5091</span>, <span class="number">-1.1356</span>],</span><br><span class="line">       [ <span class="number">1.2121</span>, <span class="number">-0.1732</span>,  <span class="number">0.1192</span>, <span class="number">-1.0442</span>],</span><br><span class="line">       [<span class="number">-0.8618</span>, <span class="number">-2.1046</span>, <span class="number">-0.4949</span>,  <span class="number">1.0718</span>],</span><br><span class="line">       [ <span class="number">0.7216</span>, <span class="number">-0.7068</span>, <span class="number">-1.0396</span>,  <span class="number">0.2719</span>],</span><br><span class="line">       [<span class="number">-0.425</span> ,  <span class="number">0.567</span> ,  <span class="number">0.2762</span>, <span class="number">-1.0874</span>],</span><br><span class="line">       [<span class="number">-0.6737</span>,  <span class="number">0.1136</span>, <span class="number">-1.4784</span>,  <span class="number">0.525</span> ]])</span><br></pre></td></tr></table></figure></div><p>3、 <code>describe()</code>函数对于数据的快速统计汇总：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">19</span>]: df.describe()</span><br><span class="line">Out[<span class="number">19</span>]: </span><br><span class="line">              A         B         C         D</span><br><span class="line">count  <span class="number">6.000000</span>  <span class="number">6.000000</span>  <span class="number">6.000000</span>  <span class="number">6.000000</span></span><br><span class="line">mean   <span class="number">0.073711</span> <span class="number">-0.431125</span> <span class="number">-0.687758</span> <span class="number">-0.233103</span></span><br><span class="line">std    <span class="number">0.843157</span>  <span class="number">0.922818</span>  <span class="number">0.779887</span>  <span class="number">0.973118</span></span><br><span class="line">min   <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">25</span>%   <span class="number">-0.611510</span> <span class="number">-0.600794</span> <span class="number">-1.368714</span> <span class="number">-1.076610</span></span><br><span class="line"><span class="number">50</span>%    <span class="number">0.022070</span> <span class="number">-0.228039</span> <span class="number">-0.767252</span> <span class="number">-0.386188</span></span><br><span class="line"><span class="number">75</span>%    <span class="number">0.658444</span>  <span class="number">0.041933</span> <span class="number">-0.034326</span>  <span class="number">0.461706</span></span><br><span class="line">max    <span class="number">1.212112</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span>  <span class="number">1.071804</span></span><br></pre></td></tr></table></figure></div><p>4、 对数据的转置：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">20</span>]: df.T</span><br><span class="line">Out[<span class="number">20</span>]: </span><br><span class="line">   <span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>  <span class="number">2013</span><span class="number">-01</span><span class="number">-06</span></span><br><span class="line">A    <span class="number">0.469112</span>    <span class="number">1.212112</span>   <span class="number">-0.861849</span>    <span class="number">0.721555</span>   <span class="number">-0.424972</span>   <span class="number">-0.673690</span></span><br><span class="line">B   <span class="number">-0.282863</span>   <span class="number">-0.173215</span>   <span class="number">-2.104569</span>   <span class="number">-0.706771</span>    <span class="number">0.567020</span>    <span class="number">0.113648</span></span><br><span class="line">C   <span class="number">-1.509059</span>    <span class="number">0.119209</span>   <span class="number">-0.494929</span>   <span class="number">-1.039575</span>    <span class="number">0.276232</span>   <span class="number">-1.478427</span></span><br><span class="line">D   <span class="number">-1.135632</span>   <span class="number">-1.044236</span>    <span class="number">1.071804</span>    <span class="number">0.271860</span>   <span class="number">-1.087401</span>    <span class="number">0.524988</span></span><br></pre></td></tr></table></figure></div><p>5、 按轴进行排序</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">21</span>]: df.sort_index(axis=<span class="number">1</span>, ascending=<span class="literal">False</span>)</span><br><span class="line">Out[<span class="number">21</span>]: </span><br><span class="line">                   D         C         B         A</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">-1.135632</span> <span class="number">-1.509059</span> <span class="number">-0.282863</span>  <span class="number">0.469112</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">-1.044236</span>  <span class="number">0.119209</span> <span class="number">-0.173215</span>  <span class="number">1.212112</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">1.071804</span> <span class="number">-0.494929</span> <span class="number">-2.104569</span> <span class="number">-0.861849</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.271860</span> <span class="number">-1.039575</span> <span class="number">-0.706771</span>  <span class="number">0.721555</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-1.087401</span>  <span class="number">0.276232</span>  <span class="number">0.567020</span> <span class="number">-0.424972</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>  <span class="number">0.524988</span> <span class="number">-1.478427</span>  <span class="number">0.113648</span> <span class="number">-0.673690</span></span><br></pre></td></tr></table></figure></div><p>6、 按值进行排序</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">22</span>]: df.sort_values(by=<span class="string">'B'</span>)</span><br><span class="line">Out[<span class="number">22</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span>  <span class="number">0.524988</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span></span><br></pre></td></tr></table></figure></div><h1 id="三、-选择"><a href="#三、-选择" class="headerlink" title="三、 选择"></a>三、 选择</h1><p>虽然标准的 Python/Numpy 的选择和设置表达式都能够直接派上用场，但是作为工程使用的代码，我们推荐使用经过优化的 pandas 数据访问方式： <code>.at</code>, <code>.iat</code>, <code>.loc</code>, <code>.iloc</code> 和 <code>.ix</code>。详情请参阅<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing" target="_blank" rel="noopener external nofollow noreferrer">索引和选取数据</a> 和 <a href="http://pandas.pydata.org/pandas-docs/stable/advanced.html#advanced" target="_blank" rel="noopener external nofollow noreferrer">多重索引/高级索引</a>。</p><h2 id="获取"><a href="#获取" class="headerlink" title="获取"></a>获取</h2><p>1、 选择一个单独的列，这将会返回一个<code>Series</code>，等同于<code>df.A</code>：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">23</span>]: df[<span class="string">'A'</span>]</span><br><span class="line">Out[<span class="number">23</span>]: </span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>    <span class="number">0.469112</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>    <span class="number">1.212112</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>   <span class="number">-0.861849</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>    <span class="number">0.721555</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>   <span class="number">-0.424972</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>   <span class="number">-0.673690</span></span><br><span class="line">Freq: D, Name: A, dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、 通过<code>[]</code>进行选择，这将会对行进行切片</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">24</span>]: df[<span class="number">0</span>:<span class="number">3</span>]</span><br><span class="line">Out[<span class="number">24</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">25</span>]: df[<span class="string">'20130102'</span>:<span class="string">'20130104'</span>]</span><br><span class="line">Out[<span class="number">25</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br></pre></td></tr></table></figure></div><h2 id="通过标签选择"><a href="#通过标签选择" class="headerlink" title="通过标签选择"></a>通过标签选择</h2><p>1、 使用标签来获取一个交叉的区域</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">26</span>]: df.loc[dates[<span class="number">0</span>]]</span><br><span class="line">Out[<span class="number">26</span>]: </span><br><span class="line">A    <span class="number">0.469112</span></span><br><span class="line">B   <span class="number">-0.282863</span></span><br><span class="line">C   <span class="number">-1.509059</span></span><br><span class="line">D   <span class="number">-1.135632</span></span><br><span class="line">Name: <span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>, dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、 通过标签来在多个轴上进行选择</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">27</span>]: df.loc[:,[<span class="string">'A'</span>,<span class="string">'B'</span>]]</span><br><span class="line">Out[<span class="number">27</span>]: </span><br><span class="line">                   A         B</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span></span><br></pre></td></tr></table></figure></div><p>3、 标签切片</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">28</span>]: df.loc[<span class="string">'20130102'</span>:<span class="string">'20130104'</span>,[<span class="string">'A'</span>,<span class="string">'B'</span>]]</span><br><span class="line">Out[<span class="number">28</span>]: </span><br><span class="line">                   A         B</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span></span><br></pre></td></tr></table></figure></div><p>4、 对于返回的对象进行维度缩减</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">29</span>]: df.loc[<span class="string">'20130102'</span>,[<span class="string">'A'</span>,<span class="string">'B'</span>]]</span><br><span class="line">Out[<span class="number">29</span>]: </span><br><span class="line">A    <span class="number">1.212112</span></span><br><span class="line">B   <span class="number">-0.173215</span></span><br><span class="line">Name: <span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>, dtype: float64</span><br></pre></td></tr></table></figure></div><p>5、 获取一个标量</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">30</span>]: df.loc[dates[<span class="number">0</span>],<span class="string">'A'</span>]</span><br><span class="line">Out[<span class="number">30</span>]: <span class="number">0.46911229990718628</span></span><br></pre></td></tr></table></figure></div><p>6、 快速访问一个标量（与上一个方法等价）</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">31</span>]: df.at[dates[<span class="number">0</span>],<span class="string">'A'</span>]</span><br><span class="line">Out[<span class="number">31</span>]: <span class="number">0.46911229990718628</span></span><br></pre></td></tr></table></figure></div><h2 id="通过位置选择"><a href="#通过位置选择" class="headerlink" title="通过位置选择"></a>通过位置选择</h2><p>1、 通过传递数值进行位置选择（选择的是行）</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">32</span>]: df.iloc[<span class="number">3</span>]</span><br><span class="line">Out[<span class="number">32</span>]: </span><br><span class="line">A    <span class="number">0.721555</span></span><br><span class="line">B   <span class="number">-0.706771</span></span><br><span class="line">C   <span class="number">-1.039575</span></span><br><span class="line">D    <span class="number">0.271860</span></span><br><span class="line">Name: <span class="number">2013</span><span class="number">-01</span><span class="number">-04</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>, dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、 通过数值进行切片，与 numpy/python 中的情况类似</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">33</span>]: df.iloc[<span class="number">3</span>:<span class="number">5</span>,<span class="number">0</span>:<span class="number">2</span>]</span><br><span class="line">Out[<span class="number">33</span>]: </span><br><span class="line">                   A         B</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span></span><br></pre></td></tr></table></figure></div><p>3、 通过指定一个位置的列表，与 numpy/python 中的情况类似</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">34</span>]: df.iloc[[<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>],[<span class="number">0</span>,<span class="number">2</span>]]</span><br><span class="line">Out[<span class="number">34</span>]: </span><br><span class="line">                   A         C</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span>  <span class="number">0.119209</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-0.494929</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.276232</span></span><br></pre></td></tr></table></figure></div><p>4、 对行进行切片</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">35</span>]: df.iloc[<span class="number">1</span>:<span class="number">3</span>,:]</span><br><span class="line">Out[<span class="number">35</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span></span><br></pre></td></tr></table></figure></div><p>5、 对列进行切片</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">36</span>]: df.iloc[:,<span class="number">1</span>:<span class="number">3</span>]</span><br><span class="line">Out[<span class="number">36</span>]: </span><br><span class="line">                   B         C</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span></span><br></pre></td></tr></table></figure></div><p>6、 获取特定的值</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">37</span>]: df.iloc[<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line">Out[<span class="number">37</span>]: <span class="number">-0.17321464905330858</span></span><br></pre></td></tr></table></figure></div><p>快速访问标量（等同于前一个方法）：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">38</span>]: df.iat[<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line">Out[<span class="number">38</span>]: <span class="number">-0.17321464905330858</span></span><br></pre></td></tr></table></figure></div><h2 id="布尔索引"><a href="#布尔索引" class="headerlink" title="布尔索引"></a>布尔索引</h2><p>1、 使用一个单独列的值来选择数据：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">39</span>]: df[df.A &gt; <span class="number">0</span>]</span><br><span class="line">Out[<span class="number">39</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span></span><br></pre></td></tr></table></figure></div><p>2、 使用<code>where</code>操作来选择数据：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">40</span>]: df[df &gt; <span class="number">0</span>]</span><br><span class="line">Out[<span class="number">40</span>]: </span><br><span class="line">                   A         B         C         D</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span>       NaN       NaN       NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span>       NaN  <span class="number">0.119209</span>       NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>       NaN       NaN       NaN  <span class="number">1.071804</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span>       NaN       NaN  <span class="number">0.271860</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>       NaN  <span class="number">0.567020</span>  <span class="number">0.276232</span>       NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>       NaN  <span class="number">0.113648</span>       NaN  <span class="number">0.524988</span></span><br></pre></td></tr></table></figure></div><p>3、 使用<code>isin()</code>方法来过滤：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">41</span>]: df2 = df.copy()</span><br><span class="line"></span><br><span class="line">In [<span class="number">42</span>]: df2[<span class="string">'E'</span>] = [<span class="string">'one'</span>, <span class="string">'one'</span>,<span class="string">'two'</span>,<span class="string">'three'</span>,<span class="string">'four'</span>,<span class="string">'three'</span>]</span><br><span class="line"></span><br><span class="line">In [<span class="number">43</span>]: df2</span><br><span class="line">Out[<span class="number">43</span>]: </span><br><span class="line">                   A         B         C         D      E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.469112</span> <span class="number">-0.282863</span> <span class="number">-1.509059</span> <span class="number">-1.135632</span>    one</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span> <span class="number">-1.044236</span>    one</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span>    two</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">0.271860</span>  three</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span>   four</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span>  <span class="number">0.524988</span>  three</span><br><span class="line"></span><br><span class="line">In [<span class="number">44</span>]: df2[df2[<span class="string">'E'</span>].isin([<span class="string">'two'</span>,<span class="string">'four'</span>])]</span><br><span class="line">Out[<span class="number">44</span>]: </span><br><span class="line">                   A         B         C         D     E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">1.071804</span>   two</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span> <span class="number">-1.087401</span>  four</span><br></pre></td></tr></table></figure></div><h2 id="设置"><a href="#设置" class="headerlink" title="设置"></a>设置</h2><p>1、 设置一个新的列：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">45</span>]: s1 = pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>], index=pd.date_range(<span class="string">'20130102'</span>, periods=<span class="number">6</span>))</span><br><span class="line"></span><br><span class="line">In [<span class="number">46</span>]: s1</span><br><span class="line">Out[<span class="number">46</span>]: </span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>    <span class="number">1</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>    <span class="number">2</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>    <span class="number">3</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>    <span class="number">4</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>    <span class="number">5</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-07</span>    <span class="number">6</span></span><br><span class="line">Freq: D, dtype: int64</span><br><span class="line"></span><br><span class="line">In [<span class="number">47</span>]: df[<span class="string">'F'</span>] = s1</span><br></pre></td></tr></table></figure></div><p>2、 通过标签设置新的值：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">48</span>]: df.at[dates[<span class="number">0</span>],<span class="string">'A'</span>] = <span class="number">0</span></span><br></pre></td></tr></table></figure></div><p>3、 通过位置设置新的值：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">49</span>]: df.iat[<span class="number">0</span>,<span class="number">1</span>] = <span class="number">0</span></span><br></pre></td></tr></table></figure></div><p>4、 通过一个numpy数组设置一组新值：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">50</span>]: df.loc[:,<span class="string">'D'</span>] = np.array([<span class="number">5</span>] * len(df))</span><br></pre></td></tr></table></figure></div><p>上述操作结果如下：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">51</span>]: df</span><br><span class="line">Out[<span class="number">51</span>]: </span><br><span class="line">                   A         B         C  D    F</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.000000</span>  <span class="number">0.000000</span> <span class="number">-1.509059</span>  <span class="number">5</span>  NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span>  <span class="number">5</span>  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">5</span>  <span class="number">2.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">5</span>  <span class="number">3.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span>  <span class="number">0.567020</span>  <span class="number">0.276232</span>  <span class="number">5</span>  <span class="number">4.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span>  <span class="number">0.113648</span> <span class="number">-1.478427</span>  <span class="number">5</span>  <span class="number">5.0</span></span><br></pre></td></tr></table></figure></div><p>5、 通过where操作来设置新的值：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">52</span>]: df2 = df.copy()</span><br><span class="line"></span><br><span class="line">In [<span class="number">53</span>]: df2[df2 &gt; <span class="number">0</span>] = -df2</span><br><span class="line"></span><br><span class="line">In [<span class="number">54</span>]: df2</span><br><span class="line">Out[<span class="number">54</span>]: </span><br><span class="line">                   A         B         C  D    F</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.000000</span>  <span class="number">0.000000</span> <span class="number">-1.509059</span> <span class="number">-5</span>  NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span> <span class="number">-1.212112</span> <span class="number">-0.173215</span> <span class="number">-0.119209</span> <span class="number">-5</span> <span class="number">-1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span> <span class="number">-5</span> <span class="number">-2.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span> <span class="number">-0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span> <span class="number">-5</span> <span class="number">-3.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-0.424972</span> <span class="number">-0.567020</span> <span class="number">-0.276232</span> <span class="number">-5</span> <span class="number">-4.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.673690</span> <span class="number">-0.113648</span> <span class="number">-1.478427</span> <span class="number">-5</span> <span class="number">-5.0</span></span><br></pre></td></tr></table></figure></div><h1 id="四、-缺失值处理"><a href="#四、-缺失值处理" class="headerlink" title="四、 缺失值处理"></a>四、 缺失值处理</h1><p>在 pandas 中，使用<code>np.nan</code>来代替缺失值，这些值将默认不会包含在计算中，详情请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/missing_data.html#missing-data" target="_blank" rel="noopener external nofollow noreferrer">缺失的数据</a>。</p><p>1、 <code>reindex()</code>方法可以对指定轴上的索引进行改变/增加/删除操作，这将返回原始数据的一个拷贝：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">55</span>]: df1 = df.reindex(index=dates[<span class="number">0</span>:<span class="number">4</span>], columns=list(df.columns) + [<span class="string">'E'</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">56</span>]: df1.loc[dates[<span class="number">0</span>]:dates[<span class="number">1</span>],<span class="string">'E'</span>] = <span class="number">1</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">57</span>]: df1</span><br><span class="line">Out[<span class="number">57</span>]: </span><br><span class="line">                   A         B         C  D    F    E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.000000</span>  <span class="number">0.000000</span> <span class="number">-1.509059</span>  <span class="number">5</span>  NaN  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span>  <span class="number">5</span>  <span class="number">1.0</span>  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">5</span>  <span class="number">2.0</span>  NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">5</span>  <span class="number">3.0</span>  NaN</span><br></pre></td></tr></table></figure></div><p>2、 去掉包含缺失值的行：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">58</span>]: df1.dropna(how=<span class="string">'any'</span>)</span><br><span class="line">Out[<span class="number">58</span>]: </span><br><span class="line">                   A         B         C  D    F    E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span>  <span class="number">5</span>  <span class="number">1.0</span>  <span class="number">1.0</span></span><br></pre></td></tr></table></figure></div><p>3、 对缺失值进行填充：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">59</span>]: df1.fillna(value=<span class="number">5</span>)</span><br><span class="line">Out[<span class="number">59</span>]: </span><br><span class="line">                   A         B         C  D    F    E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.000000</span>  <span class="number">0.000000</span> <span class="number">-1.509059</span>  <span class="number">5</span>  <span class="number">5.0</span>  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span>  <span class="number">0.119209</span>  <span class="number">5</span>  <span class="number">1.0</span>  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-0.861849</span> <span class="number">-2.104569</span> <span class="number">-0.494929</span>  <span class="number">5</span>  <span class="number">2.0</span>  <span class="number">5.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">0.721555</span> <span class="number">-0.706771</span> <span class="number">-1.039575</span>  <span class="number">5</span>  <span class="number">3.0</span>  <span class="number">5.0</span></span><br></pre></td></tr></table></figure></div><p>4、 对数据进行布尔填充：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">n [<span class="number">60</span>]: pd.isnull(df1)</span><br><span class="line">Out[<span class="number">60</span>]: </span><br><span class="line">                A      B      C      D      F      E</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>   <span class="literal">True</span>  <span class="literal">False</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>   <span class="literal">True</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>  <span class="literal">False</span>   <span class="literal">True</span></span><br></pre></td></tr></table></figure></div><h1 id="五、-相关操作"><a href="#五、-相关操作" class="headerlink" title="五、 相关操作"></a>五、 相关操作</h1><p>详情请参与 <a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-binop" target="_blank" rel="noopener external nofollow noreferrer">基本的二进制操作</a></p><h2 id="统计（相关操作通常情况下不包括缺失值）"><a href="#统计（相关操作通常情况下不包括缺失值）" class="headerlink" title="统计（相关操作通常情况下不包括缺失值）"></a>统计（相关操作通常情况下不包括缺失值）</h2><p>1、 执行描述性统计：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">61</span>]: df.mean()</span><br><span class="line">Out[<span class="number">61</span>]: </span><br><span class="line">A   <span class="number">-0.004474</span></span><br><span class="line">B   <span class="number">-0.383981</span></span><br><span class="line">C   <span class="number">-0.687758</span></span><br><span class="line">D    <span class="number">5.000000</span></span><br><span class="line">F    <span class="number">3.000000</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、 在其他轴上进行相同的操作：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">62</span>]: df.mean(<span class="number">1</span>)</span><br><span class="line">Out[<span class="number">62</span>]: </span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>    <span class="number">0.872735</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>    <span class="number">1.431621</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>    <span class="number">0.707731</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>    <span class="number">1.395042</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>    <span class="number">1.883656</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>    <span class="number">1.592306</span></span><br><span class="line">Freq: D, dtype: float64</span><br></pre></td></tr></table></figure></div><p>3、 对于拥有不同维度，需要对齐的对象进行操作。Pandas 会自动的沿着指定的维度进行广播：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">63</span>]: s = pd.Series([<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,np.nan,<span class="number">6</span>,<span class="number">8</span>], index=dates).shift(<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">64</span>]: s</span><br><span class="line">Out[<span class="number">64</span>]: </span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>    NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>    NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>    <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>    <span class="number">3.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>    <span class="number">5.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>    NaN</span><br><span class="line">Freq: D, dtype: float64</span><br><span class="line"></span><br><span class="line">In [<span class="number">65</span>]: df.sub(s, axis=<span class="string">'index'</span>)</span><br><span class="line">Out[<span class="number">65</span>]: </span><br><span class="line">                   A         B         C    D    F</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>       NaN       NaN       NaN  NaN  NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>       NaN       NaN       NaN  NaN  NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span> <span class="number">-1.861849</span> <span class="number">-3.104569</span> <span class="number">-1.494929</span>  <span class="number">4.0</span>  <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span> <span class="number">-2.278445</span> <span class="number">-3.706771</span> <span class="number">-4.039575</span>  <span class="number">2.0</span>  <span class="number">0.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span> <span class="number">-5.424972</span> <span class="number">-4.432980</span> <span class="number">-4.723768</span>  <span class="number">0.0</span> <span class="number">-1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span>       NaN       NaN       NaN  NaN  NaN</span><br></pre></td></tr></table></figure></div><h2 id="Apply"><a href="#Apply" class="headerlink" title="Apply"></a><code>Apply</code></h2><p>1、 对数据应用函数：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">66</span>]: df.apply(np.cumsum)</span><br><span class="line">Out[<span class="number">66</span>]: </span><br><span class="line">                   A         B         C   D     F</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-01</span>  <span class="number">0.000000</span>  <span class="number">0.000000</span> <span class="number">-1.509059</span>   <span class="number">5</span>   NaN</span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">1.212112</span> <span class="number">-0.173215</span> <span class="number">-1.389850</span>  <span class="number">10</span>   <span class="number">1.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">0.350263</span> <span class="number">-2.277784</span> <span class="number">-1.884779</span>  <span class="number">15</span>   <span class="number">3.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">1.071818</span> <span class="number">-2.984555</span> <span class="number">-2.924354</span>  <span class="number">20</span>   <span class="number">6.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-05</span>  <span class="number">0.646846</span> <span class="number">-2.417535</span> <span class="number">-2.648122</span>  <span class="number">25</span>  <span class="number">10.0</span></span><br><span class="line"><span class="number">2013</span><span class="number">-01</span><span class="number">-06</span> <span class="number">-0.026844</span> <span class="number">-2.303886</span> <span class="number">-4.126549</span>  <span class="number">30</span>  <span class="number">15.0</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">67</span>]: df.apply(<span class="keyword">lambda</span> x: x.max() - x.min())</span><br><span class="line">Out[<span class="number">67</span>]: </span><br><span class="line">A    <span class="number">2.073961</span></span><br><span class="line">B    <span class="number">2.671590</span></span><br><span class="line">C    <span class="number">1.785291</span></span><br><span class="line">D    <span class="number">0.000000</span></span><br><span class="line">F    <span class="number">4.000000</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure></div><h2 id="直方图"><a href="#直方图" class="headerlink" title="直方图"></a>直方图</h2><p>具体请参照：<a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-discretization" target="_blank" rel="noopener external nofollow noreferrer">直方图和离散化</a>。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">68</span>]: s = pd.Series(np.random.randint(<span class="number">0</span>, <span class="number">7</span>, size=<span class="number">10</span>))</span><br><span class="line"></span><br><span class="line">In [<span class="number">69</span>]: s</span><br><span class="line">Out[<span class="number">69</span>]: </span><br><span class="line"><span class="number">0</span>    <span class="number">4</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2</span></span><br><span class="line"><span class="number">2</span>    <span class="number">1</span></span><br><span class="line"><span class="number">3</span>    <span class="number">2</span></span><br><span class="line"><span class="number">4</span>    <span class="number">6</span></span><br><span class="line"><span class="number">5</span>    <span class="number">4</span></span><br><span class="line"><span class="number">6</span>    <span class="number">4</span></span><br><span class="line"><span class="number">7</span>    <span class="number">6</span></span><br><span class="line"><span class="number">8</span>    <span class="number">4</span></span><br><span class="line"><span class="number">9</span>    <span class="number">4</span></span><br><span class="line">dtype: int64</span><br><span class="line"></span><br><span class="line">In [<span class="number">70</span>]: s.value_counts()</span><br><span class="line">Out[<span class="number">70</span>]: </span><br><span class="line"><span class="number">4</span>    <span class="number">5</span></span><br><span class="line"><span class="number">6</span>    <span class="number">2</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2</span></span><br><span class="line"><span class="number">1</span>    <span class="number">1</span></span><br><span class="line">dtype: int64</span><br></pre></td></tr></table></figure></div><h2 id="字符串方法"><a href="#字符串方法" class="headerlink" title="字符串方法"></a>字符串方法</h2><p><code>Series</code>对象在其<code>str</code>属性中配备了一组字符串处理方法，可以很容易的应用到数组中的每个元素，如下段代码所示。更多详情请参考：<a href="http://pandas.pydata.org/pandas-docs/stable/text.html#text-string-methods" target="_blank" rel="noopener external nofollow noreferrer">字符串向量化方法</a>。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">71</span>]: s = pd.Series([<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'Aaba'</span>, <span class="string">'Baca'</span>, np.nan, <span class="string">'CABA'</span>, <span class="string">'dog'</span>, <span class="string">'cat'</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">72</span>]: s.str.lower()</span><br><span class="line">Out[<span class="number">72</span>]: </span><br><span class="line"><span class="number">0</span>       a</span><br><span class="line"><span class="number">1</span>       b</span><br><span class="line"><span class="number">2</span>       c</span><br><span class="line"><span class="number">3</span>    aaba</span><br><span class="line"><span class="number">4</span>    baca</span><br><span class="line"><span class="number">5</span>     NaN</span><br><span class="line"><span class="number">6</span>    caba</span><br><span class="line"><span class="number">7</span>     dog</span><br><span class="line"><span class="number">8</span>     cat</span><br><span class="line">dtype: object</span><br></pre></td></tr></table></figure></div><h1 id="六、-合并"><a href="#六、-合并" class="headerlink" title="六、 合并"></a>六、 合并</h1><p>Pandas 提供了大量的方法能够轻松的对<code>Series</code>，<code>DataFrame</code>和<code>Panel</code>对象进行各种符合各种逻辑关系的合并操作。具体请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/merging.html#merging" target="_blank" rel="noopener external nofollow noreferrer">合并</a>。</p><h2 id="Concat"><a href="#Concat" class="headerlink" title="Concat"></a><code>Concat</code></h2><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">73</span>]: df = pd.DataFrame(np.random.randn(<span class="number">10</span>, <span class="number">4</span>))</span><br><span class="line"></span><br><span class="line">In [<span class="number">74</span>]: df</span><br><span class="line">Out[<span class="number">74</span>]: </span><br><span class="line">          <span class="number">0</span>         <span class="number">1</span>         <span class="number">2</span>         <span class="number">3</span></span><br><span class="line"><span class="number">0</span> <span class="number">-0.548702</span>  <span class="number">1.467327</span> <span class="number">-1.015962</span> <span class="number">-0.483075</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.637550</span> <span class="number">-1.217659</span> <span class="number">-0.291519</span> <span class="number">-1.745505</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.263952</span>  <span class="number">0.991460</span> <span class="number">-0.919069</span>  <span class="number">0.266046</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.709661</span>  <span class="number">1.669052</span>  <span class="number">1.037882</span> <span class="number">-1.705775</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.919854</span> <span class="number">-0.042379</span>  <span class="number">1.247642</span> <span class="number">-0.009920</span></span><br><span class="line"><span class="number">5</span>  <span class="number">0.290213</span>  <span class="number">0.495767</span>  <span class="number">0.362949</span>  <span class="number">1.548106</span></span><br><span class="line"><span class="number">6</span> <span class="number">-1.131345</span> <span class="number">-0.089329</span>  <span class="number">0.337863</span> <span class="number">-0.945867</span></span><br><span class="line"><span class="number">7</span> <span class="number">-0.932132</span>  <span class="number">1.956030</span>  <span class="number">0.017587</span> <span class="number">-0.016692</span></span><br><span class="line"><span class="number">8</span> <span class="number">-0.575247</span>  <span class="number">0.254161</span> <span class="number">-1.143704</span>  <span class="number">0.215897</span></span><br><span class="line"><span class="number">9</span>  <span class="number">1.193555</span> <span class="number">-0.077118</span> <span class="number">-0.408530</span> <span class="number">-0.862495</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># break it into pieces</span></span><br><span class="line">In [<span class="number">75</span>]: pieces = [df[:<span class="number">3</span>], df[<span class="number">3</span>:<span class="number">7</span>], df[<span class="number">7</span>:]]</span><br><span class="line"></span><br><span class="line">In [<span class="number">76</span>]: pd.concat(pieces)</span><br><span class="line">Out[<span class="number">76</span>]: </span><br><span class="line">          <span class="number">0</span>         <span class="number">1</span>         <span class="number">2</span>         <span class="number">3</span></span><br><span class="line"><span class="number">0</span> <span class="number">-0.548702</span>  <span class="number">1.467327</span> <span class="number">-1.015962</span> <span class="number">-0.483075</span></span><br><span class="line"><span class="number">1</span>  <span class="number">1.637550</span> <span class="number">-1.217659</span> <span class="number">-0.291519</span> <span class="number">-1.745505</span></span><br><span class="line"><span class="number">2</span> <span class="number">-0.263952</span>  <span class="number">0.991460</span> <span class="number">-0.919069</span>  <span class="number">0.266046</span></span><br><span class="line"><span class="number">3</span> <span class="number">-0.709661</span>  <span class="number">1.669052</span>  <span class="number">1.037882</span> <span class="number">-1.705775</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.919854</span> <span class="number">-0.042379</span>  <span class="number">1.247642</span> <span class="number">-0.009920</span></span><br><span class="line"><span class="number">5</span>  <span class="number">0.290213</span>  <span class="number">0.495767</span>  <span class="number">0.362949</span>  <span class="number">1.548106</span></span><br><span class="line"><span class="number">6</span> <span class="number">-1.131345</span> <span class="number">-0.089329</span>  <span class="number">0.337863</span> <span class="number">-0.945867</span></span><br><span class="line"><span class="number">7</span> <span class="number">-0.932132</span>  <span class="number">1.956030</span>  <span class="number">0.017587</span> <span class="number">-0.016692</span></span><br><span class="line"><span class="number">8</span> <span class="number">-0.575247</span>  <span class="number">0.254161</span> <span class="number">-1.143704</span>  <span class="number">0.215897</span></span><br><span class="line"><span class="number">9</span>  <span class="number">1.193555</span> <span class="number">-0.077118</span> <span class="number">-0.408530</span> <span class="number">-0.862495</span></span><br></pre></td></tr></table></figure></div><h2 id="Join"><a href="#Join" class="headerlink" title="Join"></a><code>Join</code></h2><p>类似于 SQL 类型的合并，具体请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/merging.html#merging-join" target="_blank" rel="noopener external nofollow noreferrer">数据库风格的连接</a></p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">77</span>]: left = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'foo'</span>, <span class="string">'foo'</span>], <span class="string">'lval'</span>: [<span class="number">1</span>, <span class="number">2</span>]&#125;)</span><br><span class="line"></span><br><span class="line">In [<span class="number">78</span>]: right = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'foo'</span>, <span class="string">'foo'</span>], <span class="string">'rval'</span>: [<span class="number">4</span>, <span class="number">5</span>]&#125;)</span><br><span class="line"></span><br><span class="line">In [<span class="number">79</span>]: left</span><br><span class="line">Out[<span class="number">79</span>]: </span><br><span class="line">   key  lval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  foo     <span class="number">2</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">80</span>]: right</span><br><span class="line">Out[<span class="number">80</span>]: </span><br><span class="line">   key  rval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>  foo     <span class="number">5</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">81</span>]: pd.merge(left, right, on=<span class="string">'key'</span>)</span><br><span class="line">Out[<span class="number">81</span>]: </span><br><span class="line">   key  lval  rval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">1</span>     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>  foo     <span class="number">1</span>     <span class="number">5</span></span><br><span class="line"><span class="number">2</span>  foo     <span class="number">2</span>     <span class="number">4</span></span><br><span class="line"><span class="number">3</span>  foo     <span class="number">2</span>     <span class="number">5</span></span><br></pre></td></tr></table></figure></div><p>另一个例子：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">82</span>]: left = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'foo'</span>, <span class="string">'bar'</span>], <span class="string">'lval'</span>: [<span class="number">1</span>, <span class="number">2</span>]&#125;)</span><br><span class="line"></span><br><span class="line">In [<span class="number">83</span>]: right = pd.DataFrame(&#123;<span class="string">'key'</span>: [<span class="string">'foo'</span>, <span class="string">'bar'</span>], <span class="string">'rval'</span>: [<span class="number">4</span>, <span class="number">5</span>]&#125;)</span><br><span class="line"></span><br><span class="line">In [<span class="number">84</span>]: left</span><br><span class="line">Out[<span class="number">84</span>]: </span><br><span class="line">   key  lval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">1</span></span><br><span class="line"><span class="number">1</span>  bar     <span class="number">2</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">85</span>]: right</span><br><span class="line">Out[<span class="number">85</span>]: </span><br><span class="line">   key  rval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>  bar     <span class="number">5</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">86</span>]: pd.merge(left, right, on=<span class="string">'key'</span>)</span><br><span class="line">Out[<span class="number">86</span>]: </span><br><span class="line">   key  lval  rval</span><br><span class="line"><span class="number">0</span>  foo     <span class="number">1</span>     <span class="number">4</span></span><br><span class="line"><span class="number">1</span>  bar     <span class="number">2</span>     <span class="number">5</span></span><br></pre></td></tr></table></figure></div><h2 id="Append"><a href="#Append" class="headerlink" title="Append"></a><code>Append</code></h2><p>将一行连接到一个<code>DataFrame</code>上，具体请参阅<a href="http://pandas.pydata.org/pandas-docs/stable/merging.html#merging-concatenation" target="_blank" rel="noopener external nofollow noreferrer">附加</a>：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">87</span>]: df = pd.DataFrame(np.random.randn(<span class="number">8</span>, <span class="number">4</span>), columns=[<span class="string">'A'</span>,<span class="string">'B'</span>,<span class="string">'C'</span>,<span class="string">'D'</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">88</span>]: df</span><br><span class="line">Out[<span class="number">88</span>]: </span><br><span class="line">          A         B         C         D</span><br><span class="line"><span class="number">0</span>  <span class="number">1.346061</span>  <span class="number">1.511763</span>  <span class="number">1.627081</span> <span class="number">-0.990582</span></span><br><span class="line"><span class="number">1</span> <span class="number">-0.441652</span>  <span class="number">1.211526</span>  <span class="number">0.268520</span>  <span class="number">0.024580</span></span><br><span class="line"><span class="number">2</span> <span class="number">-1.577585</span>  <span class="number">0.396823</span> <span class="number">-0.105381</span> <span class="number">-0.532532</span></span><br><span class="line"><span class="number">3</span>  <span class="number">1.453749</span>  <span class="number">1.208843</span> <span class="number">-0.080952</span> <span class="number">-0.264610</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.727965</span> <span class="number">-0.589346</span>  <span class="number">0.339969</span> <span class="number">-0.693205</span></span><br><span class="line"><span class="number">5</span> <span class="number">-0.339355</span>  <span class="number">0.593616</span>  <span class="number">0.884345</span>  <span class="number">1.591431</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.141809</span>  <span class="number">0.220390</span>  <span class="number">0.435589</span>  <span class="number">0.192451</span></span><br><span class="line"><span class="number">7</span> <span class="number">-0.096701</span>  <span class="number">0.803351</span>  <span class="number">1.715071</span> <span class="number">-0.708758</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">89</span>]: s = df.iloc[<span class="number">3</span>]</span><br><span class="line"></span><br><span class="line">In [<span class="number">90</span>]: df.append(s, ignore_index=<span class="literal">True</span>)</span><br><span class="line">Out[<span class="number">90</span>]: </span><br><span class="line">          A         B         C         D</span><br><span class="line"><span class="number">0</span>  <span class="number">1.346061</span>  <span class="number">1.511763</span>  <span class="number">1.627081</span> <span class="number">-0.990582</span></span><br><span class="line"><span class="number">1</span> <span class="number">-0.441652</span>  <span class="number">1.211526</span>  <span class="number">0.268520</span>  <span class="number">0.024580</span></span><br><span class="line"><span class="number">2</span> <span class="number">-1.577585</span>  <span class="number">0.396823</span> <span class="number">-0.105381</span> <span class="number">-0.532532</span></span><br><span class="line"><span class="number">3</span>  <span class="number">1.453749</span>  <span class="number">1.208843</span> <span class="number">-0.080952</span> <span class="number">-0.264610</span></span><br><span class="line"><span class="number">4</span> <span class="number">-0.727965</span> <span class="number">-0.589346</span>  <span class="number">0.339969</span> <span class="number">-0.693205</span></span><br><span class="line"><span class="number">5</span> <span class="number">-0.339355</span>  <span class="number">0.593616</span>  <span class="number">0.884345</span>  <span class="number">1.591431</span></span><br><span class="line"><span class="number">6</span>  <span class="number">0.141809</span>  <span class="number">0.220390</span>  <span class="number">0.435589</span>  <span class="number">0.192451</span></span><br><span class="line"><span class="number">7</span> <span class="number">-0.096701</span>  <span class="number">0.803351</span>  <span class="number">1.715071</span> <span class="number">-0.708758</span></span><br><span class="line"><span class="number">8</span>  <span class="number">1.453749</span>  <span class="number">1.208843</span> <span class="number">-0.080952</span> <span class="number">-0.264610</span></span><br></pre></td></tr></table></figure></div><h1 id="七、-分组"><a href="#七、-分组" class="headerlink" title="七、 分组"></a>七、 分组</h1><p>对于”group by”操作，我们通常是指以下一个或多个操作步骤：</p><ul><li><p>（Splitting）按照一些规则将数据分为不同的组；</p></li><li><p>（Applying）对于每组数据分别执行一个函数；</p></li><li><p>（Combining）将结果组合到一个数据结构中；</p></li></ul><p>详情请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#groupby" target="_blank" rel="noopener external nofollow noreferrer">_Grouping section_</a></p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">91</span>]: df = pd.DataFrame(&#123;<span class="string">'A'</span> : [<span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>,</span><br><span class="line">   ....:                           <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>],</span><br><span class="line">   ....:                    <span class="string">'B'</span> : [<span class="string">'one'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>,</span><br><span class="line">   ....:                           <span class="string">'two'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'three'</span>],</span><br><span class="line">   ....:                    <span class="string">'C'</span> : np.random.randn(<span class="number">8</span>),</span><br><span class="line">   ....:                    <span class="string">'D'</span> : np.random.randn(<span class="number">8</span>)&#125;)</span><br><span class="line">   ....: </span><br><span class="line"></span><br><span class="line">In [<span class="number">92</span>]: df</span><br><span class="line">Out[<span class="number">92</span>]: </span><br><span class="line">     A      B         C         D</span><br><span class="line"><span class="number">0</span>  foo    one <span class="number">-1.202872</span> <span class="number">-0.055224</span></span><br><span class="line"><span class="number">1</span>  bar    one <span class="number">-1.814470</span>  <span class="number">2.395985</span></span><br><span class="line"><span class="number">2</span>  foo    two  <span class="number">1.018601</span>  <span class="number">1.552825</span></span><br><span class="line"><span class="number">3</span>  bar  three <span class="number">-0.595447</span>  <span class="number">0.166599</span></span><br><span class="line"><span class="number">4</span>  foo    two  <span class="number">1.395433</span>  <span class="number">0.047609</span></span><br><span class="line"><span class="number">5</span>  bar    two <span class="number">-0.392670</span> <span class="number">-0.136473</span></span><br><span class="line"><span class="number">6</span>  foo    one  <span class="number">0.007207</span> <span class="number">-0.561757</span></span><br><span class="line"><span class="number">7</span>  foo  three  <span class="number">1.928123</span> <span class="number">-1.623033</span></span><br></pre></td></tr></table></figure></div><p>1、 分组并对每个分组执行<code>sum</code>函数：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">93</span>]: df.groupby(<span class="string">'A'</span>).sum()</span><br><span class="line">Out[<span class="number">93</span>]: </span><br><span class="line">            C        D</span><br><span class="line">A                     </span><br><span class="line">bar <span class="number">-2.802588</span>  <span class="number">2.42611</span></span><br><span class="line">foo  <span class="number">3.146492</span> <span class="number">-0.63958</span></span><br></pre></td></tr></table></figure></div><p>2、 通过多个列进行分组形成一个层次索引，然后执行函数：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">94</span>]: df.groupby([<span class="string">'A'</span>,<span class="string">'B'</span>]).sum()</span><br><span class="line">Out[<span class="number">94</span>]: </span><br><span class="line">                  C         D</span><br><span class="line">A   B                        </span><br><span class="line">bar one   <span class="number">-1.814470</span>  <span class="number">2.395985</span></span><br><span class="line">    three <span class="number">-0.595447</span>  <span class="number">0.166599</span></span><br><span class="line">    two   <span class="number">-0.392670</span> <span class="number">-0.136473</span></span><br><span class="line">foo one   <span class="number">-1.195665</span> <span class="number">-0.616981</span></span><br><span class="line">    three  <span class="number">1.928123</span> <span class="number">-1.623033</span></span><br><span class="line">    two    <span class="number">2.414034</span>  <span class="number">1.600434</span></span><br></pre></td></tr></table></figure></div><h1 id="八、-改变形状"><a href="#八、-改变形状" class="headerlink" title="八、 改变形状"></a>八、 改变形状</h1><p>详情请参阅 <a href="http://pandas.pydata.org/pandas-docs/stable/advanced.html#advanced-hierarchical" target="_blank" rel="noopener external nofollow noreferrer">层次索引</a> 和 <a href="http://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-stacking" target="_blank" rel="noopener external nofollow noreferrer">改变形状</a>。</p><h2 id="Stack"><a href="#Stack" class="headerlink" title="Stack"></a><code>Stack</code></h2><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">95</span>]: tuples = list(zip(*[[<span class="string">'bar'</span>, <span class="string">'bar'</span>, <span class="string">'baz'</span>, <span class="string">'baz'</span>,</span><br><span class="line">   ....:                      <span class="string">'foo'</span>, <span class="string">'foo'</span>, <span class="string">'qux'</span>, <span class="string">'qux'</span>],</span><br><span class="line">   ....:                     [<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>,</span><br><span class="line">   ....:                      <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>]]))</span><br><span class="line">   ....: </span><br><span class="line"></span><br><span class="line">In [<span class="number">96</span>]: index = pd.MultiIndex.from_tuples(tuples, names=[<span class="string">'first'</span>, <span class="string">'second'</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">97</span>]: df = pd.DataFrame(np.random.randn(<span class="number">8</span>, <span class="number">2</span>), index=index, columns=[<span class="string">'A'</span>, <span class="string">'B'</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">98</span>]: df2 = df[:<span class="number">4</span>]</span><br><span class="line"></span><br><span class="line">In [<span class="number">99</span>]: df2</span><br><span class="line">Out[<span class="number">99</span>]: </span><br><span class="line">                     A         B</span><br><span class="line">first second                    </span><br><span class="line">bar   one     <span class="number">0.029399</span> <span class="number">-0.542108</span></span><br><span class="line">      two     <span class="number">0.282696</span> <span class="number">-0.087302</span></span><br><span class="line">baz   one    <span class="number">-1.575170</span>  <span class="number">1.771208</span></span><br><span class="line">      two     <span class="number">0.816482</span>  <span class="number">1.100230</span></span><br></pre></td></tr></table></figure></div><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">100</span>]: stacked = df2.stack()</span><br><span class="line"></span><br><span class="line">In [<span class="number">101</span>]: stacked</span><br><span class="line">Out[<span class="number">101</span>]: </span><br><span class="line">first  second   </span><br><span class="line">bar    one     A    <span class="number">0.029399</span></span><br><span class="line">               B   <span class="number">-0.542108</span></span><br><span class="line">       two     A    <span class="number">0.282696</span></span><br><span class="line">               B   <span class="number">-0.087302</span></span><br><span class="line">baz    one     A   <span class="number">-1.575170</span></span><br><span class="line">               B    <span class="number">1.771208</span></span><br><span class="line">       two     A    <span class="number">0.816482</span></span><br><span class="line">               B    <span class="number">1.100230</span></span><br><span class="line">dtype: float64</span><br></pre></td></tr></table></figure></div><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">102</span>]: stacked.unstack()</span><br><span class="line">Out[<span class="number">102</span>]: </span><br><span class="line">                     A         B</span><br><span class="line">first second                    </span><br><span class="line">bar   one     <span class="number">0.029399</span> <span class="number">-0.542108</span></span><br><span class="line">      two     <span class="number">0.282696</span> <span class="number">-0.087302</span></span><br><span class="line">baz   one    <span class="number">-1.575170</span>  <span class="number">1.771208</span></span><br><span class="line">      two     <span class="number">0.816482</span>  <span class="number">1.100230</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">103</span>]: stacked.unstack(<span class="number">1</span>)</span><br><span class="line">Out[<span class="number">103</span>]: </span><br><span class="line">second        one       two</span><br><span class="line">first                      </span><br><span class="line">bar   A  <span class="number">0.029399</span>  <span class="number">0.282696</span></span><br><span class="line">      B <span class="number">-0.542108</span> <span class="number">-0.087302</span></span><br><span class="line">baz   A <span class="number">-1.575170</span>  <span class="number">0.816482</span></span><br><span class="line">      B  <span class="number">1.771208</span>  <span class="number">1.100230</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">104</span>]: stacked.unstack(<span class="number">0</span>)</span><br><span class="line">Out[<span class="number">104</span>]: </span><br><span class="line">first          bar       baz</span><br><span class="line">second                      </span><br><span class="line">one    A  <span class="number">0.029399</span> <span class="number">-1.575170</span></span><br><span class="line">       B <span class="number">-0.542108</span>  <span class="number">1.771208</span></span><br><span class="line">two    A  <span class="number">0.282696</span>  <span class="number">0.816482</span></span><br><span class="line">       B <span class="number">-0.087302</span>  <span class="number">1.100230</span></span><br></pre></td></tr></table></figure></div><h2 id="数据透视表"><a href="#数据透视表" class="headerlink" title="数据透视表"></a>数据透视表</h2><p>详情请参阅：<a href="http://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-pivot" target="_blank" rel="noopener external nofollow noreferrer">数据透视表</a>.</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">105</span>]: df = pd.DataFrame(&#123;<span class="string">'A'</span> : [<span class="string">'one'</span>, <span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>] * <span class="number">3</span>,</span><br><span class="line">   .....:                    <span class="string">'B'</span> : [<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>] * <span class="number">4</span>,</span><br><span class="line">   .....:                    <span class="string">'C'</span> : [<span class="string">'foo'</span>, <span class="string">'foo'</span>, <span class="string">'foo'</span>, <span class="string">'bar'</span>, <span class="string">'bar'</span>, <span class="string">'bar'</span>] * <span class="number">2</span>,</span><br><span class="line">   .....:                    <span class="string">'D'</span> : np.random.randn(<span class="number">12</span>),</span><br><span class="line">   .....:                    <span class="string">'E'</span> : np.random.randn(<span class="number">12</span>)&#125;)</span><br><span class="line">   .....: </span><br><span class="line"></span><br><span class="line">In [<span class="number">106</span>]: df</span><br><span class="line">Out[<span class="number">106</span>]: </span><br><span class="line">        A  B    C         D         E</span><br><span class="line"><span class="number">0</span>     one  A  foo  <span class="number">1.418757</span> <span class="number">-0.179666</span></span><br><span class="line"><span class="number">1</span>     one  B  foo <span class="number">-1.879024</span>  <span class="number">1.291836</span></span><br><span class="line"><span class="number">2</span>     two  C  foo  <span class="number">0.536826</span> <span class="number">-0.009614</span></span><br><span class="line"><span class="number">3</span>   three  A  bar  <span class="number">1.006160</span>  <span class="number">0.392149</span></span><br><span class="line"><span class="number">4</span>     one  B  bar <span class="number">-0.029716</span>  <span class="number">0.264599</span></span><br><span class="line"><span class="number">5</span>     one  C  bar <span class="number">-1.146178</span> <span class="number">-0.057409</span></span><br><span class="line"><span class="number">6</span>     two  A  foo  <span class="number">0.100900</span> <span class="number">-1.425638</span></span><br><span class="line"><span class="number">7</span>   three  B  foo <span class="number">-1.035018</span>  <span class="number">1.024098</span></span><br><span class="line"><span class="number">8</span>     one  C  foo  <span class="number">0.314665</span> <span class="number">-0.106062</span></span><br><span class="line"><span class="number">9</span>     one  A  bar <span class="number">-0.773723</span>  <span class="number">1.824375</span></span><br><span class="line"><span class="number">10</span>    two  B  bar <span class="number">-1.170653</span>  <span class="number">0.595974</span></span><br><span class="line"><span class="number">11</span>  three  C  bar  <span class="number">0.648740</span>  <span class="number">1.167115</span></span><br></pre></td></tr></table></figure></div><p>可以从这个数据中轻松的生成数据透视表：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">107</span>]: pd.pivot_table(df, values=<span class="string">'D'</span>, index=[<span class="string">'A'</span>, <span class="string">'B'</span>], columns=[<span class="string">'C'</span>])</span><br><span class="line">Out[<span class="number">107</span>]: </span><br><span class="line">C             bar       foo</span><br><span class="line">A     B                    </span><br><span class="line">one   A <span class="number">-0.773723</span>  <span class="number">1.418757</span></span><br><span class="line">      B <span class="number">-0.029716</span> <span class="number">-1.879024</span></span><br><span class="line">      C <span class="number">-1.146178</span>  <span class="number">0.314665</span></span><br><span class="line">three A  <span class="number">1.006160</span>       NaN</span><br><span class="line">      B       NaN <span class="number">-1.035018</span></span><br><span class="line">      C  <span class="number">0.648740</span>       NaN</span><br><span class="line">two   A       NaN  <span class="number">0.100900</span></span><br><span class="line">      B <span class="number">-1.170653</span>       NaN</span><br><span class="line">      C       NaN  <span class="number">0.536826</span></span><br></pre></td></tr></table></figure></div><h1 id="九、-时间序列"><a href="#九、-时间序列" class="headerlink" title="九、 时间序列"></a>九、 时间序列</h1><p>Pandas 在对频率转换进行重新采样时拥有简单、强大且高效的功能（如将按秒采样的数据转换为按5分钟为单位进行采样的数据）。这种操作在金融领域非常常见。具体参考：<a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html#timeseries" target="_blank" rel="noopener external nofollow noreferrer">时间序列</a>。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">108</span>]: rng = pd.date_range(<span class="string">'1/1/2012'</span>, periods=<span class="number">100</span>, freq=<span class="string">'S'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">109</span>]: ts = pd.Series(np.random.randint(<span class="number">0</span>, <span class="number">500</span>, len(rng)), index=rng)</span><br><span class="line"></span><br><span class="line">In [<span class="number">110</span>]: ts.resample(<span class="string">'5Min'</span>).sum()</span><br><span class="line">Out[<span class="number">110</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span>    <span class="number">25083</span></span><br><span class="line">Freq: <span class="number">5</span>T, dtype: int64</span><br></pre></td></tr></table></figure></div><p>1、 时区表示：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">111</span>]: rng = pd.date_range(<span class="string">'3/6/2012 00:00'</span>, periods=<span class="number">5</span>, freq=<span class="string">'D'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">112</span>]: ts = pd.Series(np.random.randn(len(rng)), rng)</span><br><span class="line"></span><br><span class="line">In [<span class="number">113</span>]: ts</span><br><span class="line">Out[<span class="number">113</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-06</span>    <span class="number">0.464000</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-07</span>    <span class="number">0.227371</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-08</span>   <span class="number">-0.496922</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-09</span>    <span class="number">0.306389</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-10</span>   <span class="number">-2.290613</span></span><br><span class="line">Freq: D, dtype: float64</span><br><span class="line"></span><br><span class="line">In [<span class="number">114</span>]: ts_utc = ts.tz_localize(<span class="string">'UTC'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">115</span>]: ts_utc</span><br><span class="line">Out[<span class="number">115</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-06</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>+<span class="number">00</span>:<span class="number">00</span>    <span class="number">0.464000</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-07</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>+<span class="number">00</span>:<span class="number">00</span>    <span class="number">0.227371</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-08</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>+<span class="number">00</span>:<span class="number">00</span>   <span class="number">-0.496922</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-09</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>+<span class="number">00</span>:<span class="number">00</span>    <span class="number">0.306389</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-10</span> <span class="number">00</span>:<span class="number">00</span>:<span class="number">00</span>+<span class="number">00</span>:<span class="number">00</span>   <span class="number">-2.290613</span></span><br><span class="line">Freq: D, dtype: float64</span><br></pre></td></tr></table></figure></div><p>2、 时区转换：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">116</span>]: ts_utc.tz_convert(<span class="string">'US/Eastern'</span>)</span><br><span class="line">Out[<span class="number">116</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-05</span> <span class="number">19</span>:<span class="number">00</span>:<span class="number">00</span><span class="number">-05</span>:<span class="number">00</span>    <span class="number">0.464000</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-06</span> <span class="number">19</span>:<span class="number">00</span>:<span class="number">00</span><span class="number">-05</span>:<span class="number">00</span>    <span class="number">0.227371</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-07</span> <span class="number">19</span>:<span class="number">00</span>:<span class="number">00</span><span class="number">-05</span>:<span class="number">00</span>   <span class="number">-0.496922</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-08</span> <span class="number">19</span>:<span class="number">00</span>:<span class="number">00</span><span class="number">-05</span>:<span class="number">00</span>    <span class="number">0.306389</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-09</span> <span class="number">19</span>:<span class="number">00</span>:<span class="number">00</span><span class="number">-05</span>:<span class="number">00</span>   <span class="number">-2.290613</span></span><br><span class="line">Freq: D, dtype: float64</span><br></pre></td></tr></table></figure></div><p>3、 时间跨度转换：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">117</span>]: rng = pd.date_range(<span class="string">'1/1/2012'</span>, periods=<span class="number">5</span>, freq=<span class="string">'M'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">118</span>]: ts = pd.Series(np.random.randn(len(rng)), index=rng)</span><br><span class="line"></span><br><span class="line">In [<span class="number">119</span>]: ts</span><br><span class="line">Out[<span class="number">119</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-31</span>   <span class="number">-1.134623</span></span><br><span class="line"><span class="number">2012</span><span class="number">-02</span><span class="number">-29</span>   <span class="number">-1.561819</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-31</span>   <span class="number">-0.260838</span></span><br><span class="line"><span class="number">2012</span><span class="number">-04</span><span class="number">-30</span>    <span class="number">0.281957</span></span><br><span class="line"><span class="number">2012</span><span class="number">-05</span><span class="number">-31</span>    <span class="number">1.523962</span></span><br><span class="line">Freq: M, dtype: float64</span><br><span class="line"></span><br><span class="line">In [<span class="number">120</span>]: ps = ts.to_period()</span><br><span class="line"></span><br><span class="line">In [<span class="number">121</span>]: ps</span><br><span class="line">Out[<span class="number">121</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-01</span>   <span class="number">-1.134623</span></span><br><span class="line"><span class="number">2012</span><span class="number">-02</span>   <span class="number">-1.561819</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span>   <span class="number">-0.260838</span></span><br><span class="line"><span class="number">2012</span><span class="number">-04</span>    <span class="number">0.281957</span></span><br><span class="line"><span class="number">2012</span><span class="number">-05</span>    <span class="number">1.523962</span></span><br><span class="line">Freq: M, dtype: float64</span><br><span class="line"></span><br><span class="line">In [<span class="number">122</span>]: ps.to_timestamp()</span><br><span class="line">Out[<span class="number">122</span>]: </span><br><span class="line"><span class="number">2012</span><span class="number">-01</span><span class="number">-01</span>   <span class="number">-1.134623</span></span><br><span class="line"><span class="number">2012</span><span class="number">-02</span><span class="number">-01</span>   <span class="number">-1.561819</span></span><br><span class="line"><span class="number">2012</span><span class="number">-03</span><span class="number">-01</span>   <span class="number">-0.260838</span></span><br><span class="line"><span class="number">2012</span><span class="number">-04</span><span class="number">-01</span>    <span class="number">0.281957</span></span><br><span class="line"><span class="number">2012</span><span class="number">-05</span><span class="number">-01</span>    <span class="number">1.523962</span></span><br><span class="line">Freq: MS, dtype: float64</span><br></pre></td></tr></table></figure></div><p>4、 时期和时间戳之间的转换使得可以使用一些方便的算术函数。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">123</span>]: prng = pd.period_range(<span class="string">'1990Q1'</span>, <span class="string">'2000Q4'</span>, freq=<span class="string">'Q-NOV'</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">124</span>]: ts = pd.Series(np.random.randn(len(prng)), prng)</span><br><span class="line"></span><br><span class="line">In [<span class="number">125</span>]: ts.index = (prng.asfreq(<span class="string">'M'</span>, <span class="string">'e'</span>) + <span class="number">1</span>).asfreq(<span class="string">'H'</span>, <span class="string">'s'</span>) + <span class="number">9</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">126</span>]: ts.head()</span><br><span class="line">Out[<span class="number">126</span>]: </span><br><span class="line"><span class="number">1990</span><span class="number">-03</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>   <span class="number">-0.902937</span></span><br><span class="line"><span class="number">1990</span><span class="number">-06</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>    <span class="number">0.068159</span></span><br><span class="line"><span class="number">1990</span><span class="number">-09</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>   <span class="number">-0.057873</span></span><br><span class="line"><span class="number">1990</span><span class="number">-12</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>   <span class="number">-0.368204</span></span><br><span class="line"><span class="number">1991</span><span class="number">-03</span><span class="number">-01</span> <span class="number">09</span>:<span class="number">00</span>   <span class="number">-1.144073</span></span><br><span class="line">Freq: H, dtype: float64</span><br></pre></td></tr></table></figure></div><h1 id="十、-Categorical"><a href="#十、-Categorical" class="headerlink" title="十、 Categorical"></a>十、 Categorical</h1><p>从 0.15 版本开始，pandas 可以在<code>DataFrame</code>中支持 Categorical 类型的数据，详细 介绍参看：<a href="http://pandas.pydata.org/pandas-docs/stable/categorical.html#categorical" target="_blank" rel="noopener external nofollow noreferrer">Categorical 简介</a>和<a href="http://pandas.pydata.org/pandas-docs/stable/api.html#api-categorical" target="_blank" rel="noopener external nofollow noreferrer">_API documentation_</a>。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">127</span>]: df = pd.DataFrame(&#123;<span class="string">"id"</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>], <span class="string">"raw_grade"</span>:[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'b'</span>, <span class="string">'a'</span>, <span class="string">'a'</span>, <span class="string">'e'</span>]&#125;)</span><br></pre></td></tr></table></figure></div><p>1、 将原始的<code>grade</code>转换为 Categorical 数据类型：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">128</span>]: df[<span class="string">"grade"</span>] = df[<span class="string">"raw_grade"</span>].astype(<span class="string">"category"</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">129</span>]: df[<span class="string">"grade"</span>]</span><br><span class="line">Out[<span class="number">129</span>]: </span><br><span class="line"><span class="number">0</span>    a</span><br><span class="line"><span class="number">1</span>    b</span><br><span class="line"><span class="number">2</span>    b</span><br><span class="line"><span class="number">3</span>    a</span><br><span class="line"><span class="number">4</span>    a</span><br><span class="line"><span class="number">5</span>    e</span><br><span class="line">Name: grade, dtype: category</span><br><span class="line">Categories (<span class="number">3</span>, object): [a, b, e]</span><br></pre></td></tr></table></figure></div><p>2、 将 Categorical 类型数据重命名为更有意义的名称：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">130</span>]: df[<span class="string">"grade"</span>].cat.categories = [<span class="string">"very good"</span>, <span class="string">"good"</span>, <span class="string">"very bad"</span>]</span><br></pre></td></tr></table></figure></div><p>3、 对类别进行重新排序，增加缺失的类别：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">131</span>]: df[<span class="string">"grade"</span>] = df[<span class="string">"grade"</span>].cat.set_categories([<span class="string">"very bad"</span>, <span class="string">"bad"</span>, <span class="string">"medium"</span>, <span class="string">"good"</span>, <span class="string">"very good"</span>])</span><br><span class="line"></span><br><span class="line">In [<span class="number">132</span>]: df[<span class="string">"grade"</span>]</span><br><span class="line">Out[<span class="number">132</span>]: </span><br><span class="line"><span class="number">0</span>    very good</span><br><span class="line"><span class="number">1</span>         good</span><br><span class="line"><span class="number">2</span>         good</span><br><span class="line"><span class="number">3</span>    very good</span><br><span class="line"><span class="number">4</span>    very good</span><br><span class="line"><span class="number">5</span>     very bad</span><br><span class="line">Name: grade, dtype: category</span><br><span class="line">Categories (<span class="number">5</span>, object): [very bad, bad, medium, good, very good]</span><br></pre></td></tr></table></figure></div><p>4、 排序是按照 Categorical 的顺序进行的而不是按照字典顺序进行：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">133</span>]: df.sort_values(by=<span class="string">"grade"</span>)</span><br><span class="line">Out[<span class="number">133</span>]: </span><br><span class="line">   id raw_grade      grade</span><br><span class="line"><span class="number">5</span>   <span class="number">6</span>         e   very bad</span><br><span class="line"><span class="number">1</span>   <span class="number">2</span>         b       good</span><br><span class="line"><span class="number">2</span>   <span class="number">3</span>         b       good</span><br><span class="line"><span class="number">0</span>   <span class="number">1</span>         a  very good</span><br><span class="line"><span class="number">3</span>   <span class="number">4</span>         a  very good</span><br><span class="line"><span class="number">4</span>   <span class="number">5</span>         a  very good</span><br></pre></td></tr></table></figure></div><p>5、 对 Categorical 列进行排序时存在空的类别：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">134</span>]: df.groupby(<span class="string">"grade"</span>).size()</span><br><span class="line">Out[<span class="number">134</span>]: </span><br><span class="line">grade</span><br><span class="line">very bad     <span class="number">1</span></span><br><span class="line">bad          <span class="number">0</span></span><br><span class="line">medium       <span class="number">0</span></span><br><span class="line">good         <span class="number">2</span></span><br><span class="line">very good    <span class="number">3</span></span><br><span class="line">dtype: int64</span><br></pre></td></tr></table></figure></div><h1 id="十一、-画图"><a href="#十一、-画图" class="headerlink" title="十一、 画图"></a>十一、 画图</h1><p>具体文档参看：<a href="http://pandas.pydata.org/pandas-docs/stable/visualization.html#visualization" target="_blank" rel="noopener external nofollow noreferrer">绘图</a>文档。</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">135</span>]: ts = pd.Series(np.random.randn(<span class="number">1000</span>), index=pd.date_range(<span class="string">'1/1/2000'</span>, periods=<span class="number">1000</span>))</span><br><span class="line"></span><br><span class="line">In [<span class="number">136</span>]: ts = ts.cumsum()</span><br><span class="line"></span><br><span class="line">In [<span class="number">137</span>]: ts.plot()</span><br><span class="line">Out[<span class="number">137</span>]: &lt;matplotlib.axes._subplots.AxesSubplot at <span class="number">0x7ff2ab2af550</span>&gt;</span><br></pre></td></tr></table></figure></div><p><img src="http://pandas.pydata.org/pandas-docs/stable/_images/series_plot_basic.png" alt></p><p>对于<code>DataFrame</code>来说，<code>plot</code>是一种将所有列及其标签进行绘制的简便方法：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">138</span>]: df = pd.DataFrame(np.random.randn(<span class="number">1000</span>, <span class="number">4</span>), index=ts.index,</span><br><span class="line">   .....:                   columns=[<span class="string">'A'</span>, <span class="string">'B'</span>, <span class="string">'C'</span>, <span class="string">'D'</span>])</span><br><span class="line">   .....: </span><br><span class="line"></span><br><span class="line">In [<span class="number">139</span>]: df = df.cumsum()</span><br><span class="line"></span><br><span class="line">In [<span class="number">140</span>]: plt.figure(); df.plot(); plt.legend(loc=<span class="string">'best'</span>)</span><br><span class="line">Out[<span class="number">140</span>]: &lt;matplotlib.legend.Legend at <span class="number">0x7ff29c8163d0</span>&gt;</span><br></pre></td></tr></table></figure></div><p><img src="http://pandas.pydata.org/pandas-docs/stable/_images/frame_plot_basic.png" alt></p><h1 id="十二、-导入和保存数据"><a href="#十二、-导入和保存数据" class="headerlink" title="十二、 导入和保存数据"></a>十二、 导入和保存数据</h1><h2 id="CSV"><a href="#CSV" class="headerlink" title="CSV"></a>CSV</h2><p>参考：<a href="http://pandas.pydata.org/pandas-docs/stable/io.html#io-store-in-csv" target="_blank" rel="noopener external nofollow noreferrer">写入 CSV 文件</a>。</p><p>1、 写入 csv 文件：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">141</span>]: df.to_csv(<span class="string">'foo.csv'</span>)</span><br></pre></td></tr></table></figure></div><p>2、 从 csv 文件中读取：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">142</span>]: pd.read_csv(<span class="string">'foo.csv'</span>)</span><br><span class="line">Out[<span class="number">142</span>]: </span><br><span class="line">     Unnamed: <span class="number">0</span>          A          B         C          D</span><br><span class="line"><span class="number">0</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-01</span>   <span class="number">0.266457</span>  <span class="number">-0.399641</span> <span class="number">-0.219582</span>   <span class="number">1.186860</span></span><br><span class="line"><span class="number">1</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">-1.170732</span>  <span class="number">-0.345873</span>  <span class="number">1.653061</span>  <span class="number">-0.282953</span></span><br><span class="line"><span class="number">2</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">-1.734933</span>   <span class="number">0.530468</span>  <span class="number">2.060811</span>  <span class="number">-0.515536</span></span><br><span class="line"><span class="number">3</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">-1.555121</span>   <span class="number">1.452620</span>  <span class="number">0.239859</span>  <span class="number">-1.156896</span></span><br><span class="line"><span class="number">4</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-05</span>   <span class="number">0.578117</span>   <span class="number">0.511371</span>  <span class="number">0.103552</span>  <span class="number">-2.428202</span></span><br><span class="line"><span class="number">5</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-06</span>   <span class="number">0.478344</span>   <span class="number">0.449933</span> <span class="number">-0.741620</span>  <span class="number">-1.962409</span></span><br><span class="line"><span class="number">6</span>    <span class="number">2000</span><span class="number">-01</span><span class="number">-07</span>   <span class="number">1.235339</span>  <span class="number">-0.091757</span> <span class="number">-1.543861</span>  <span class="number">-1.084753</span></span><br><span class="line">..          ...        ...        ...       ...        ...</span><br><span class="line"><span class="number">993</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-20</span> <span class="number">-10.628548</span>  <span class="number">-9.153563</span> <span class="number">-7.883146</span>  <span class="number">28.313940</span></span><br><span class="line"><span class="number">994</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-21</span> <span class="number">-10.390377</span>  <span class="number">-8.727491</span> <span class="number">-6.399645</span>  <span class="number">30.914107</span></span><br><span class="line"><span class="number">995</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-22</span>  <span class="number">-8.985362</span>  <span class="number">-8.485624</span> <span class="number">-4.669462</span>  <span class="number">31.367740</span></span><br><span class="line"><span class="number">996</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-23</span>  <span class="number">-9.558560</span>  <span class="number">-8.781216</span> <span class="number">-4.499815</span>  <span class="number">30.518439</span></span><br><span class="line"><span class="number">997</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-24</span>  <span class="number">-9.902058</span>  <span class="number">-9.340490</span> <span class="number">-4.386639</span>  <span class="number">30.105593</span></span><br><span class="line"><span class="number">998</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-25</span> <span class="number">-10.216020</span>  <span class="number">-9.480682</span> <span class="number">-3.933802</span>  <span class="number">29.758560</span></span><br><span class="line"><span class="number">999</span>  <span class="number">2002</span><span class="number">-09</span><span class="number">-26</span> <span class="number">-11.856774</span> <span class="number">-10.671012</span> <span class="number">-3.216025</span>  <span class="number">29.369368</span></span><br><span class="line"></span><br><span class="line">[<span class="number">1000</span> rows x <span class="number">5</span> columns]</span><br></pre></td></tr></table></figure></div><h2 id="HDF5"><a href="#HDF5" class="headerlink" title="HDF5"></a>HDF5</h2><p>参考：<a href="http://pandas.pydata.org/pandas-docs/stable/io.html#io-hdf5" target="_blank" rel="noopener external nofollow noreferrer">HDF5 存储</a></p><p>1、 写入 HDF5 存储：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">143</span>]: df.to_hdf(<span class="string">'foo.h5'</span>,<span class="string">'df'</span>)</span><br></pre></td></tr></table></figure></div><p>2、 从 HDF5 存储中读取：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">144</span>]: pd.read_hdf(<span class="string">'foo.h5'</span>,<span class="string">'df'</span>)</span><br><span class="line">Out[<span class="number">144</span>]: </span><br><span class="line">                    A          B         C          D</span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-01</span>   <span class="number">0.266457</span>  <span class="number">-0.399641</span> <span class="number">-0.219582</span>   <span class="number">1.186860</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">-1.170732</span>  <span class="number">-0.345873</span>  <span class="number">1.653061</span>  <span class="number">-0.282953</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">-1.734933</span>   <span class="number">0.530468</span>  <span class="number">2.060811</span>  <span class="number">-0.515536</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">-1.555121</span>   <span class="number">1.452620</span>  <span class="number">0.239859</span>  <span class="number">-1.156896</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-05</span>   <span class="number">0.578117</span>   <span class="number">0.511371</span>  <span class="number">0.103552</span>  <span class="number">-2.428202</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-06</span>   <span class="number">0.478344</span>   <span class="number">0.449933</span> <span class="number">-0.741620</span>  <span class="number">-1.962409</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-07</span>   <span class="number">1.235339</span>  <span class="number">-0.091757</span> <span class="number">-1.543861</span>  <span class="number">-1.084753</span></span><br><span class="line"><span class="meta">... </span>              ...        ...       ...        ...</span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-20</span> <span class="number">-10.628548</span>  <span class="number">-9.153563</span> <span class="number">-7.883146</span>  <span class="number">28.313940</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-21</span> <span class="number">-10.390377</span>  <span class="number">-8.727491</span> <span class="number">-6.399645</span>  <span class="number">30.914107</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-22</span>  <span class="number">-8.985362</span>  <span class="number">-8.485624</span> <span class="number">-4.669462</span>  <span class="number">31.367740</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-23</span>  <span class="number">-9.558560</span>  <span class="number">-8.781216</span> <span class="number">-4.499815</span>  <span class="number">30.518439</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-24</span>  <span class="number">-9.902058</span>  <span class="number">-9.340490</span> <span class="number">-4.386639</span>  <span class="number">30.105593</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-25</span> <span class="number">-10.216020</span>  <span class="number">-9.480682</span> <span class="number">-3.933802</span>  <span class="number">29.758560</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-26</span> <span class="number">-11.856774</span> <span class="number">-10.671012</span> <span class="number">-3.216025</span>  <span class="number">29.369368</span></span><br><span class="line"></span><br><span class="line">[<span class="number">1000</span> rows x <span class="number">4</span> columns]</span><br></pre></td></tr></table></figure></div><h2 id="Excel"><a href="#Excel" class="headerlink" title="Excel"></a>Excel</h2><p>参考：<a href="http://pandas.pydata.org/pandas-docs/stable/io.html#io-excel" target="_blank" rel="noopener external nofollow noreferrer">_MS Excel_</a></p><p>1、 写入excel文件：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">145</span>]: df.to_excel(<span class="string">'foo.xlsx'</span>, sheet_name=<span class="string">'Sheet1'</span>)</span><br></pre></td></tr></table></figure></div><p>2、 从excel文件中读取：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">146</span>]: pd.read_excel(<span class="string">'foo.xlsx'</span>, <span class="string">'Sheet1'</span>, index_col=<span class="literal">None</span>, na_values=[<span class="string">'NA'</span>])</span><br><span class="line">Out[<span class="number">146</span>]: </span><br><span class="line">                    A          B         C          D</span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-01</span>   <span class="number">0.266457</span>  <span class="number">-0.399641</span> <span class="number">-0.219582</span>   <span class="number">1.186860</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-02</span>  <span class="number">-1.170732</span>  <span class="number">-0.345873</span>  <span class="number">1.653061</span>  <span class="number">-0.282953</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-03</span>  <span class="number">-1.734933</span>   <span class="number">0.530468</span>  <span class="number">2.060811</span>  <span class="number">-0.515536</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-04</span>  <span class="number">-1.555121</span>   <span class="number">1.452620</span>  <span class="number">0.239859</span>  <span class="number">-1.156896</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-05</span>   <span class="number">0.578117</span>   <span class="number">0.511371</span>  <span class="number">0.103552</span>  <span class="number">-2.428202</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-06</span>   <span class="number">0.478344</span>   <span class="number">0.449933</span> <span class="number">-0.741620</span>  <span class="number">-1.962409</span></span><br><span class="line"><span class="number">2000</span><span class="number">-01</span><span class="number">-07</span>   <span class="number">1.235339</span>  <span class="number">-0.091757</span> <span class="number">-1.543861</span>  <span class="number">-1.084753</span></span><br><span class="line"><span class="meta">... </span>              ...        ...       ...        ...</span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-20</span> <span class="number">-10.628548</span>  <span class="number">-9.153563</span> <span class="number">-7.883146</span>  <span class="number">28.313940</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-21</span> <span class="number">-10.390377</span>  <span class="number">-8.727491</span> <span class="number">-6.399645</span>  <span class="number">30.914107</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-22</span>  <span class="number">-8.985362</span>  <span class="number">-8.485624</span> <span class="number">-4.669462</span>  <span class="number">31.367740</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-23</span>  <span class="number">-9.558560</span>  <span class="number">-8.781216</span> <span class="number">-4.499815</span>  <span class="number">30.518439</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-24</span>  <span class="number">-9.902058</span>  <span class="number">-9.340490</span> <span class="number">-4.386639</span>  <span class="number">30.105593</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-25</span> <span class="number">-10.216020</span>  <span class="number">-9.480682</span> <span class="number">-3.933802</span>  <span class="number">29.758560</span></span><br><span class="line"><span class="number">2002</span><span class="number">-09</span><span class="number">-26</span> <span class="number">-11.856774</span> <span class="number">-10.671012</span> <span class="number">-3.216025</span>  <span class="number">29.369368</span></span><br><span class="line"></span><br><span class="line">[<span class="number">1000</span> rows x <span class="number">4</span> columns]</span><br></pre></td></tr></table></figure></div><h1 id="十三、陷阱"><a href="#十三、陷阱" class="headerlink" title="十三、陷阱"></a>十三、陷阱</h1><p>如果你尝试某个操作并且看到如下异常：</p><div class="highlight-wrap" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true" data-rel="PY"><figure class="iseeu highlight /py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">if</span> pd.Series([<span class="literal">False</span>, <span class="literal">True</span>, <span class="literal">False</span>]):</span><br><span class="line">    print(<span class="string">"I was true"</span>)</span><br><span class="line">Traceback</span><br><span class="line">    ...</span><br><span class="line">ValueError: The truth value of an array <span class="keyword">is</span> ambiguous. Use a.empty, a.any() <span class="keyword">or</span> a.all().</span><br></pre></td></tr></table></figure></div><p>解释及处理方式请见<a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#basics-compare" target="_blank" rel="noopener external nofollow noreferrer">比较</a>。</p><p>同时请见<a href="http://pandas.pydata.org/pandas-docs/stable/gotchas.html#gotchas" target="_blank" rel="noopener external nofollow noreferrer">陷阱</a>。</p></div><div class="popular-posts-header"><i class="fa fa-graduation-cap"></i> 相关文章</div><details><summary>点击查看</summary><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\blog\pandas中文手册.html" rel="bookmark">Pandas中文手册</a></div></li></ul></details><div><div class="my_post_copyright"><script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script><script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script><p><span>本文标题:</span>十分钟搞定 pandas</p><p><span>文章作者:</span>TJYS</p><p><span>发布时间:</span>2020年03月28日 - 00:00:00</p><p><span>最后更新:</span>2020年04月04日 - 16:35:09</p><p><span>原始链接:</span><a href="/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html" title="十分钟搞定 pandas">http://sky_mirrors_the_clouds.gitee.io/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html</a> <span class="copy-path" title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://sky_mirrors_the_clouds.gitee.io/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html" aria-label="复制成功！"></i></span></p><p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license external nofollow noreferrer" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p></div><script>var clipboard=new Clipboard(".fa-clipboard");$(".fa-clipboard").click(function(){clipboard.on("success",function(){swal({title:"",text:"复制成功",icon:"success",showConfirmButton:!0})})})</script></div><div><div><div class="read-over">-------------------本文结束 <i class="fa fa-paw"></i> 感谢您的阅读-------------------</div></div></div><div><div class="share_reward"><div>坚持原创技术分享，感谢您的支持和鼓励！</div><button id="rewardButton" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span>打赏</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/images/wechatpay.png" alt="TJYS 微信支付"><p>微信支付</p></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/images/alipay.jpg" alt="TJYS 支付宝"><p>支付宝</p></div></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/blog/tags/pandas/" rel="tag"><i class="fa fa-tag"></i> pandas</a></div><div class="post-widgets"><div class="wp_rating"><div style="color:rgba(0,0,0,.75);font-size:13px;letter-spacing:4px;margin-bottom:5px">(&gt;看完记得五星好评哦亲&lt;)</div><div id="wpac-rating"></div></div></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/blog/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E8%AF%BE%E7%A8%8B.html" rel="next" title="机器学习课程"><i class="fa fa-chevron-left"></i> 机器学习课程</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/blog/pandas%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C.html" rel="prev" title="Pandas中文手册">Pandas中文手册 <i class="fa fa-chevron-right"></i></a></div></div></footer></div></article><div class="post-spread"><div data-weibo-title="分享到微博" data-qq-title="分享到QQ" data-douban-title="分享到豆瓣" class="social-share" class="share-component" data-disabled="qzone,google+,linkedin" data-description="Share.js - 一键分享到微博，QQ空间，腾讯微博，人人，豆瓣...">分享到：</div></div></div></div><div class="comments" id="comments"></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div id="sidebar-dimmer"></div><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap">站点概览</li></ul><section class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><a href="/"><img class="site-author-image" itemprop="image" src="/blog/images/avatar.jpg" alt="TJYS"></a><p class="site-author-name" itemprop="name">TJYS</p><p class="site-description motion-element" itemprop="description"></p></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"><a href="/blog/archives/%7C%7Carchive"><a href="/blog/archives"><span class="site-state-item-count">6</span> <span class="site-state-item-name">文章</span></a></a></div><div class="site-state-item site-state-categories"><a href="/blog/categories/index.html"><span class="site-state-item-count">9</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/blog/tags/index.html"><span class="site-state-item-count">4</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="feed-link motion-element"><a href="/blog/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS </a><a title="收藏到书签，偶尔High一下^_^" rel="alternate external nofollow noreferrer" class="mw-harlem_shake_slow wobble shake" href="javascript:void(0)" onclick="(    /*     * Copyright (C) 2015 Rocko (rocko.xyz) <rocko.zxp@gmail.com>     *     * Licensed under the Apache License, Version 2.0 (the 'License');     * you may not use this file except in compliance with the License.     * You may obtain a copy of the License at     *     *      http://www.apache.org/licenses/LICENSE-2.0     *     * Unless required by applicable law or agreed to in writing, software     * distributed under the License is distributed on an 'AS IS' BASIS,     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     * See the License for the specific language governing permissions and     * limitations under the License.     */    function go() {        function c() {        var e = document.createElement('link');        e.setAttribute('type', 'text/css');        e.setAttribute('rel', 'stylesheet');        e.setAttribute('href', f);        e.setAttribute('class', l);        document.body.appendChild(e)    }     function h() {        var e = document.getElementsByClassName(l);        for (var t = 0; t < e.length; t++) {            document.body.removeChild(e[t])        }    }     function p() {        var e = document.createElement('div');        e.setAttribute('class', a);        document.body.appendChild(e);        setTimeout(function() {            document.body.removeChild(e)        }, 100)    }     function d(e) {        return {            height : e.offsetHeight,            width : e.offsetWidth        }    }     function v(i) {        var s = d(i);        return s.height > e &amp;&amp; s.height < n &amp;&amp; s.width > t &amp;&amp; s.width < r    }     function m(e) {        var t = e;        var n = 0;        while (!!t) {            n += t.offsetTop;            t = t.offsetParent        }        return n    }     function g() {        var e = document.documentElement;        if (!!window.innerWidth) {            return window.innerHeight        } else if (e &amp;&amp; !isNaN(e.clientHeight)) {            return e.clientHeight        }        return 0    }     function y() {        if (window.pageYOffset) {            return window.pageYOffset        }        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)    }     function E(e) {        var t = m(e);        return t >= w &amp;&amp; t <= b + w    }     var songs = [                'http://s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3', 'http://www.ytmp3.cn/down/57563.mp3'  ];    function S() {        var e = document.getElementById('audio_element_id');        if(e != null){            var index = parseInt(e.getAttribute('curSongIndex'));            if(index > songs.length - 2) {                index = 0;            } else {                index++;            }            e.setAttribute('curSongIndex', index);            N();        }        e.src = i;        e.play()    }     function x(e) {        e.className += ' ' + s + ' ' + o    }     function T(e) {        e.className += ' ' + s + ' ' + u[Math.floor(Math.random() * u.length)]    }     function N() {        var e = document.getElementsByClassName(s);        var t = new RegExp('\\b' + s + '\\b');        for (var n = 0; n < e.length; ) {            e[n].className = e[n].className.replace(t, '')        }    }    function initAudioEle() {        var e = document.getElementById('audio_element_id');        if(e === null){            e = document.createElement('audio');            e.setAttribute('class', l);            e.setAttribute('curSongIndex', 0);            e.id = 'audio_element_id';            e.loop = false;            e.bgcolor = 0;            e.addEventListener('canplay', function() {            setTimeout(function() {                x(k)            }, 500);            setTimeout(function() {                N();                p();                for (var e = 0; e < O.length; e++) {                    T(O[e])                }            }, 15500)        }, true);        e.addEventListener('ended', function() {            N();            h();            go();        }, true);        e.innerHTML = ' <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>';        document.body.appendChild(e);        }    }        initAudioEle();    var e = 30;    var t = 30;    var n = 350;    var r = 350;    var curSongIndex = parseInt(document.getElementById('audio_element_id').getAttribute('curSongIndex'));    var i = songs[curSongIndex];        var s = 'mw-harlem_shake_me';    var o = 'im_first';    var u = ['im_drunk', 'im_baked', 'im_trippin', 'im_blown'];    var a = 'mw-strobe_light';    var f = 'https://rocko.xyz/css/harlem-shake-style.css';        var l = 'mw_added_css';    var b = g();    var w = y();    var C = document.getElementsByTagName('*');    var k = null;    for (var L = 0; L < C.length; L++) {        var A = C[L];        if (v(A)) {            if (E(A)) {                k = A;                break            }        }    }    if (A === null) {        console.warn('Could not find a node of the right size. Please try a different page.');        return    }    c();    S();    var O = [];    for (var L = 0; L < C.length; L++) {        var A = C[L];        if (v(A)) {            O.push(A)        }    }    })()"><i class="fa fa-music"></i> High~</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a rel="external nofollow" href="https://github.com/Qikaile" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a rel="external nofollow" href="mailto：qikaile15205626407@gmail.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a> </span><span class="links-of-author-item"><a rel="external nofollow" href="https://user.qzone.qq.com/2649019047" target="_blank" title="QQ"><i class="fa fa-fw fa-qq"></i>QQ</a> </span><span class="links-of-author-item"><a rel="external nofollow" href="https://www.facebook.com/profile.php?id=100035074688152" target="_blank" title="facebook"><i class="fa fa-fw fa-facebook"></i>facebook</a></span></div><div id="music163player"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="280" height="86" src="//music.163.com/outchain/player?type=2&id=28802028&auto=0&height=66"></iframe></div><div class="cc-license motion-element" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" class="cc-opacity" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a></div><marquee><a href="/links"><font size="4" face="STXingkai"><font color="#D1002E">点</font> <font color="#BA0045">我</font> <font color="#FF0000">欢</font> <font color="#E80017">迎</font> <font color="#A3005C">互</font> <font color="#A00073">换</font> <font color="#A0008A">友</font> <font color="#A000A1">链</font> <font color="#A000B8">哦</font> <font color="#A000CF">~</font> <font color="#A000E6">~</font></font></a></marquee><div class="links-of-blogroll motion-element links-of-blogroll-block"><div class="links-of-blogroll-title"><i class="fa fa-history fa-" aria-hidden="true"></i> 近期文章</div><ul class="links-of-blogroll-list"><li class="my-links-of-blogroll-li"><a href="/blog/PS%E5%8A%9F%E8%83%BD%E7%B2%BE%E9%80%9A%E8%AF%BE.html" title="PS功能精通课" target="_blank">PS功能精通课</a></li><li class="my-links-of-blogroll-li"><a href="/blog/pandas%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C.html" title="Pandas中文手册" target="_blank">Pandas中文手册</a></li><li class="my-links-of-blogroll-li"><a href="/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html" title="十分钟搞定 pandas" target="_blank">十分钟搞定 pandas</a></li><li class="my-links-of-blogroll-li"><a href="/blog/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E8%AF%BE%E7%A8%8B.html" title="机器学习课程" target="_blank">机器学习课程</a></li><li class="my-links-of-blogroll-li"><a href="/blog/hexo-theme-beautify.html" title="Hexo搭建个人博客系列：主题美化篇" target="_blank">Hexo搭建个人博客系列：主题美化篇</a></li></ul></div><div><canvas id="canvas" style="width:60%"></canvas></div><script>!function(){function t(t){var r=[];a.fillStyle="#005eac";var h=new Date,u=70,s=30,v=h.getHours(),g=Math.floor(v/10),m=v%10;r.push({num:g}),r.push({num:m}),r.push({num:10});var c=h.getMinutes(),g=Math.floor(c/10),m=c%10;r.push({num:g}),r.push({num:m}),r.push({num:10});var M=h.getSeconds(),g=Math.floor(M/10),m=M%10;r.push({num:g}),r.push({num:m});for(var p=0;p<r.length;p++)r[p].offsetX=u,u=f(u,s,r[p].num,t),p<r.length-1&&10!=r[p].num&&10!=r[p+1].num&&(u+=l);if(0==i.length)i=r;else for(var C=0;C<i.length;C++)i[C].num!=r[C].num&&(n(r[C]),i[C].num=r[C].num);return e(t),o(),h}function n(t){for(var n=t.num,e=m[n],o=0;o<e.length;o++)for(var f=0;f<e[o].length;f++)if(1==e[o][f]){var a={offsetX:t.offsetX+u+2*u*f,offsetY:30+u+2*u*o,color:g[Math.floor(Math.random()*g.length)],g:1.5+Math.random(),vx:4*Math.pow(-1,Math.ceil(10*Math.random()))+Math.random(),vy:-5};v.push(a)}}function e(t){for(var n=0;n<v.length;n++)t.beginPath(),t.fillStyle=v[n].color,t.arc(v[n].offsetX,v[n].offsetY,u,0,2*Math.PI),t.fill()}function o(){for(var t=0,n=0;n<v.length;n++){var e=v[n];e.offsetX+=e.vx,e.offsetY+=e.vy,e.vy+=e.g,e.offsetY>h-u&&(e.offsetY=h-u,e.vy=-e.vy*s),e.offsetX>u&&e.offsetX<r-u&&(v[t]=v[n],t++)}for(;t<v.length;t++)v.pop()}function f(t,n,e,o){for(var f=m[e],a=0;a<f.length;a++)for(var r=0;r<f[a].length;r++)1==f[a][r]&&(o.beginPath(),o.arc(t+u+2*u*r,n+u+2*u*a,u,0,2*Math.PI),o.fill());return o.beginPath(),t+=f[0].length*u*2}var a,r=820,h=250,u=7,l=10,s=.65,v=[];const g=["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"];var i=[],m=[[[0,0,1,1,1,0,0],[0,1,1,0,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,0,1,1,0],[0,0,1,1,1,0,0]],[[0,0,0,1,1,0,0],[0,1,1,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[1,1,1,1,1,1,1]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,1,1,0,0,0],[0,1,1,0,0,0,0],[1,1,0,0,0,0,0],[1,1,0,0,0,1,1],[1,1,1,1,1,1,1]],[[1,1,1,1,1,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,1,1,1,0,0],[0,0,0,0,1,1,0],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,0,0,0,1,1,0],[0,0,0,1,1,1,0],[0,0,1,1,1,1,0],[0,1,1,0,1,1,0],[1,1,0,0,1,1,0],[1,1,1,1,1,1,1],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,1,1,1,1]],[[1,1,1,1,1,1,1],[1,1,0,0,0,0,0],[1,1,0,0,0,0,0],[1,1,1,1,1,1,0],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,0,0,0,1,1,0],[0,0,1,1,0,0,0],[0,1,1,0,0,0,0],[1,1,0,0,0,0,0],[1,1,0,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[1,1,1,1,1,1,1],[1,1,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,0,0,1,1,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0],[0,0,1,1,0,0,0]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,1,1,0]],[[0,1,1,1,1,1,0],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[1,1,0,0,0,1,1],[0,1,1,1,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,0,1,1],[0,0,0,0,1,1,0],[0,0,0,1,1,0,0],[0,1,1,0,0,0,0]],[[0,0,0,0],[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,0,0,0]]],c=document.getElementById("canvas");c.width=r,c.height=h,a=c.getContext("2d");new Date;setInterval(function(){a.clearRect(0,0,a.canvas.width,a.canvas.height),t(a)},50)}()</script><div id="days"></div><script language="javascript">function show_date_time(){window.setTimeout("show_date_time()",1e3),BirthDay=new Date("03/14/2020 00:00:00"),today=new Date,timeold=today.getTime()-BirthDay.getTime(),sectimeold=timeold/1e3,secondsold=Math.floor(sectimeold),msPerDay=864e5,e_daysold=timeold/msPerDay,daysold=Math.floor(e_daysold),e_hrsold=24*(e_daysold-daysold),hrsold=setzero(Math.floor(e_hrsold)),e_minsold=60*(e_hrsold-hrsold),minsold=setzero(Math.floor(60*(e_hrsold-hrsold))),seconds=setzero(Math.floor(60*(e_minsold-minsold))),document.getElementById("days").innerHTML="已运行"+daysold+"天"+hrsold+"时"+minsold+"分"+seconds+"秒"}function setzero(e){return 10>e&&(e="0"+e),e}show_date_time()</script></div></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#一、-创建对象"><span class="nav-number">1.</span> <span class="nav-text">一、 创建对象</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、-查看数据"><span class="nav-number">2.</span> <span class="nav-text">二、 查看数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、-选择"><span class="nav-number">3.</span> <span class="nav-text">三、 选择</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#获取"><span class="nav-number">3.1.</span> <span class="nav-text">获取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#通过标签选择"><span class="nav-number">3.2.</span> <span class="nav-text">通过标签选择</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#通过位置选择"><span class="nav-number">3.3.</span> <span class="nav-text">通过位置选择</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#布尔索引"><span class="nav-number">3.4.</span> <span class="nav-text">布尔索引</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#设置"><span class="nav-number">3.5.</span> <span class="nav-text">设置</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#四、-缺失值处理"><span class="nav-number">4.</span> <span class="nav-text">四、 缺失值处理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#五、-相关操作"><span class="nav-number">5.</span> <span class="nav-text">五、 相关操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#统计（相关操作通常情况下不包括缺失值）"><span class="nav-number">5.1.</span> <span class="nav-text">统计（相关操作通常情况下不包括缺失值）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Apply"><span class="nav-number">5.2.</span> <span class="nav-text">Apply</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#直方图"><span class="nav-number">5.3.</span> <span class="nav-text">直方图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字符串方法"><span class="nav-number">5.4.</span> <span class="nav-text">字符串方法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#六、-合并"><span class="nav-number">6.</span> <span class="nav-text">六、 合并</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Concat"><span class="nav-number">6.1.</span> <span class="nav-text">Concat</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Join"><span class="nav-number">6.2.</span> <span class="nav-text">Join</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Append"><span class="nav-number">6.3.</span> <span class="nav-text">Append</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#七、-分组"><span class="nav-number">7.</span> <span class="nav-text">七、 分组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#八、-改变形状"><span class="nav-number">8.</span> <span class="nav-text">八、 改变形状</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Stack"><span class="nav-number">8.1.</span> <span class="nav-text">Stack</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据透视表"><span class="nav-number">8.2.</span> <span class="nav-text">数据透视表</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#九、-时间序列"><span class="nav-number">9.</span> <span class="nav-text">九、 时间序列</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#十、-Categorical"><span class="nav-number">10.</span> <span class="nav-text">十、 Categorical</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#十一、-画图"><span class="nav-number">11.</span> <span class="nav-text">十一、 画图</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#十二、-导入和保存数据"><span class="nav-number">12.</span> <span class="nav-text">十二、 导入和保存数据</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#CSV"><span class="nav-number">12.1.</span> <span class="nav-text">CSV</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#HDF5"><span class="nav-number">12.2.</span> <span class="nav-text">HDF5</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Excel"><span class="nav-number">12.3.</span> <span class="nav-text">Excel</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#十三、陷阱"><span class="nav-number">13.</span> <span class="nav-text">十三、陷阱</span></a></li></ol></div></div></section></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div class="copyright">Copyright&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love" id="animate"><i class="fa fa-heartbeat"></i> </span><span class="author" itemprop="copyrightHolder">TJYS.&ensp;</span> <span title="博客总字数"><i class="fa fa-edit"></i>&ensp;<span class="post-count">47.3k</span>字</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="symbols_count_time.count_total">124k</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="symbols_count_time.time_total">1:53</span></div><span id="sitetime"></span><script language="javascript">function siteTime(){window.setTimeout("siteTime()",1e3);var e=1e3,t=60*e,o=60*t,i=24*o,a=365*i,n=new Date,r=n.getFullYear(),l=n.getMonth()+1,s=n.getDate(),M=n.getHours(),g=n.getMinutes(),m=n.getSeconds(),T=Date.UTC(2020,3,14,0,0,0),f=Date.UTC(r,l,s,M,g,m),h=f-T,u=Math.floor(h/a),d=Math.floor(h/i-365*u),D=Math.floor((h-(365*u+d)*i)/o),c=Math.floor((h-(365*u+d)*i-D*o)/t),w=Math.floor((h-(365*u+d)*i-D*o-c*t)/e);document.getElementById("sitetime").innerHTML=" 我已在此等候你 "+d+" 天 "+D+" 小时 "+c+" 分钟 "+w+" 秒"}siteTime()</script><div class="BbeiAn-info"><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=34030402000254" style="text-decoration:none;padding-left:30px;background:url(https://gitee.com/sky_mirrors_the_clouds/cloudimg/raw/master/https://gitee.com/sky_mirrors_the_clouds/cloudimg/公安.png) no-repeat left center" rel="nofollow">皖公网安备 34030402000254号 </a>| 皖ICP备 - <a target="_blank" href="http://www.beian.miit.gov.cn/" rel="nofollow">20020535号</a></div><div class="weixin-box"><div class="weixin-menu"><div class="weixin-hover"><div class="weixin-description">微信扫一扫，在线沟通</div></div></div></div></div></footer><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span id="scrollpercent"><span>0</span>%</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/velocity-animate@1.2.1/velocity.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/npm/velocity-animate@1.2.1/velocity.ui.min.js"></script><script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script><script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/gh/cungudafa/cdn/js/Valine.min.js"></script><style>.v .vwrap{padding:0 0 44px}.v .veditor{min-height:10rem;background-image:url(https://cdn.jsdelivr.net/gh/honjun/cdn@1.6/img/other/comment-bg.png);background-size:contain;background-repeat:no-repeat;background-position:right;background-color:rgba(255,255,255,0);resize:none}.v .vwrap .vedit{padding-top:20px}.v .vwrap .vheader{width:80%;bottom:0;position:absolute;background:#fff7f7f7}.v .vinput{padding:10px 15px;text-align:center}.v .vwrap .vheader .vinput{border-bottom:0}.v .vwrap .vedit .vctrl{margin-top:-44px;right:0;position:absolute;margin-right:-3px}.v .vwrap .vcontrol{position:absolute;right:0;bottom:0;width:20%;padding-top:0}.v .vwrap .vcontrol .col.col-20,.v .vwrap .vedit .vctrl span.vpreview-btn{display:none}.v .vwrap .vcontrol .col.col-80{width:100%}.v .vbtn.vsubmit{border-radius:0;padding:0;color:#fff;line-height:44px;width:100%;border:none;background:#f99}.v .vlist .vcard .vhead .vsys{background:#fff7f7f7}@media screen and (max-width:520px){.v .vwrap .vheader .vinput{width:33.33%;padding:10px 5px}}</style><script type="text/javascript">new Valine({
        lang: 'zh-cn',
        admin_email: '2649019047@qq.com', //博主邮箱
        el: '#comments' ,
        notify: true, //邮件提醒！
        appId: 'QJxW5v8BlvRP9LYqYmrNRekF-MdYXbMMI',
        appKey: 'YtfMVlMfTdU4Y4zTo9k0Pj3h',
        avatar: 'monsterid',//小怪物头像,
  });

  //   自定义邮箱审核规则
    document.body.addEventListener('click', function(e) {
        if (e.target.classList.contains('vsubmit')) {
            const email = document.querySelector('input[type=email]');
            const nick = document.querySelector('input[name=nick]');
            const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
            if (!email.value || !nick.value || !reg.test(email.value)) {
                const str = `<div class="valert text-center"><div class="vtext">请填写正确的昵称和邮箱！</div></div>`;
                const vmark = document.querySelector('.vmark');
                vmark.innerHTML = str;
                vmark.style.display = 'block';
                setTimeout(function() {
                    vmark.style.display = 'none';
                    vmark.innerHTML = '';
                }, 2500);
            }
        }
    });

 // 点击回复直接评论,官方版本点击回复时都是跳回到页面上方的评论框进行回复，评论框是固定不动的
    // 参考https://immmmm.com/valine-diy,用到jQuery
    $(document).ready(function(){
        //$('.vemoji-btn').text('😀');
        $("#vcomments").on('click', 'span.vat',function(){
            $(this).parent('div.vmeta').next("div.vcontent").after($("div.vwrap"));
            $('textarea#veditor').focus();
        })
    })</script><script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script><script type="text/javascript">jinrishici.load(function(t){var i=t.data.content+"\n「"+t.data.origin.title+"」"+t.data.origin.dynasty+" · "+t.data.origin.author;document.getElementById("veditor").setAttribute("placeholder",i)})</script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":/json$/i.test(search_path)&&(isXml=!1);var path="/blog/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var $=[];0!=g.length;){var C=g[g.length-1],m=C.position,x=C.word,w=m-20,y=m+80;0>w&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),$.push(r(p,w,y,g))}$.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&($=$.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",$.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script><script src="https://www.gstatic.com/firebasejs/4.6.0/firebase.js"></script><script src="https://www.gstatic.com/firebasejs/4.6.0/firebase-firestore.js"></script><script>!function(){function getCount(t,e,n){return t.get().then(function(a){var i;return a.exists?(i=a.data().count,n&&(window.localStorage&&window.localStorage.getItem(title)||(t.set({count:i+1,url:e}),i++))):n?(t.set({count:1,url:e}),i=1):i=0,window.localStorage&&n&&localStorage.setItem(title,!0),i})}function appendCountTo(t){return function(e){$(t).append($("<span>").addClass("post-visitors-count").append($("<span>").addClass("post-meta-divider").text("|")).append($("<span>").addClass("post-meta-item-icon").append($("<i>").addClass("fa fa-eye"))).append($("<span>").text("浏览 "+e+" 次")))}}firebase.initializeApp({apiKey:"AIzaSyDht9QEIa3UkAq8kKf2cgxOwBxFJ8-0vtc",projectId:"tjsy20200408"});var db=firebase.firestore(),articles=db.collection("articles"),isPost="十分钟搞定 pandas".length>0,isArchive=!1,isCategory="".length>0,isTag="".length>0,urlPath="十分钟搞定pandas.html",urlFullPath="http://sky_mirrors_the_clouds.gitee.io/blog/%E5%8D%81%E5%88%86%E9%92%9F%E6%90%9E%E5%AE%9Apandas.html",indexPath="index.html",isMenu=!1,menuLink="/blog/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/about/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/tags/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/categories/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/archives/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/shuoshuo/";(urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0);var menuLink="/blog/sitemap.xml";if((urlPath.indexOf(menuLink)>0||urlPath==indexPath)&&(isMenu=!0),isMenu){if(urlPath==indexPath){var titles=[],postsstr="";eval(postsstr);var promises=titles.map(function(t){return articles.doc(t)}).map(function(t){return getCount(t)});Promise.all(promises).then(function(t){var e=$(".post-wordcount");t.forEach(function(t,n){appendCountTo(e[n])(t)})})}}else{var title="十分钟搞定 pandas",doc=articles.doc(title);getCount(doc,urlFullPath,!0).then(appendCountTo($(".post-wordcount")))}}()</script><script>!function(){var t=document.createElement("script"),s=window.location.protocol.split(":")[0];"https"===s?t.src="https://zz.bdstatic.com/linksubmit/push.js":t.src="http://push.zhanzhang.baidu.com/push.js";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}()</script><script type="text/javascript">wpac_init=window.wpac_init||[],wpac_init.push({widget:"Rating",id:23975,el:"wpac-rating",color:"ff9800"}),function(){if(!("WIDGETPACK_LOADED"in window)){WIDGETPACK_LOADED=!0;var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="//embed.widgetpack.com/widget.js";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t.nextSibling)}}()</script><script type="text/x-mathjax-config">MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });</script><script type="text/x-mathjax-config">MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });</script><script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script><script src="https://cdn.jsdelivr.net/gh/theme-next/theme-next-bookmark@latest/bookmark.min.js"></script><script type="text/javascript">bookmark.scrollToMark("manual","#more")</script><script src="https://script-1256884783.file.myqcloud.com/cursor/love.min.js"></script><script src="https://script-1256884783.file.myqcloud.com/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE)</script><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.1/js/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.1/js/clipboard-use.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.3/css/sharejs.css"><script src="https://cdn.jsdelivr.net/gh/qikaile/cdn@1.3/js/social-share.min.js"></script></body></html><!-- rebuild by neat -->